ARRAY INVERSION
Problem Description
Number of an inversion in array is the number of pair(a[i],a[j]) of elements such that i < j and a[i] > a[j]. For an example if we have a list of element 2 3 6 9 1 then number of inversion is 4 and the pairs are (2,1), (3,1), (6,1) and (9,1).
Input:
The first line is N, N is the size of array.
The second line contains N input C[i].
CODING ARENA::
#include <stdio.h>
int arrinv(int ar[], int a)
{
int i,j,c=0;
for(i=0;i<a;i++)
{
for(j=i+1;j<a;j++)
{
if(ar[i]>ar[j])
c++;
}
}
return c;
}
int main()
{
int a,i;
scanf("%d",&a);
int arr[a];
for(i=0;i<=a;i++)
{
scanf("%d",&arr[i]);
}
printf("%d",arrinv(arr,a));
return 0;
}
Test Case 1
Input (stdin)5
2 3 6 9 1
Expected Output
4
Test Case 2
Input (stdin)5
1 2 3 5 6
Expected Output
0
No comments:
Post a Comment