Code:
#include<stdio.h>
#include<conio.h>
main()
{
int n,a[100],i,j,count=0;
printf("Enter total no of elements in permutation");
scanf("%d",&n);
printf("Enter all the elements of permutation");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
count=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n-1;j++)
{
if(a[i]>a[j])
count++;
}
}
printf("%d",count);
return 0;
getch();
}
Hi, Im just trying to compare the first element of array with the subsequent remaining elements, if the first element is greater than the subsequently compared elements, Count is incremented by 1. For each subsequent element that is less than the compared element we increment by 1..Next, take the 2nd element and compare it with the subsequent elements, if it is greater, increment count by 1.. This carries on until the end of the array..
What's the problem with the above code?