196,969 views
13 votes
13 votes
Let a be an int array and let i and j be two valid indices of a, a pair (i,j) with i a[j]. Design a method that counts the different pairs of indices of a in disarray. For example, the array <1,0,3,2> has two pairs in disarray.

User Pfunc
by
3.4k points

1 Answer

10 votes
10 votes

Answer:

Code:-

#include <iostream>

using namespace std;

int main()

{

// take array size from user

int n;

cout << "Enter array size:";

cin >> n;

// Declare array size with user given input

int a[n];

// take array elements from user & store it in array

cout << "Enter array elements:";

for(int i=0 ; i < n ; i++){

cin >> a[i];

}

/* ----- Solution Starts ----- */

// i & j for checking disarray & count is for counting no. of disarray's

int i=1, j=0, count=0;

for(int k=0; k < n ; k++){

if(a[i] > a[j]){

// if disarray found then increament count by 1

count++;

}

// increament i & j also to check for further disarray's

i++;

j++;

}

// Print the disarray count

cout << "Pairs of disarray in given array:" << count;

/* ----- Solution Ends ----- */

return 0;

}

Output:-

Let a be an int array and let i and j be two valid indices of a, a pair (i,j) with-example-1
User Brandon Yang
by
3.1k points