You are on page 1of 3

S2 BTech

C Programming
Code-Chef Submission

Name: ANANDHA KRISHNAN. R


Roll No: AM.EN.U4ECE17109

QUESTION: 4: LEPERMUT
EASY
The Little Elephant likes permutations. This time he has a
permutation A[1], A[2], ..., A[N] of numbers 1, 2, ..., N.

He calls a permutation A good, if the number of its inversions is equal


to the number of its local inversions. The number of inversions is
equal to the number of pairs of integers (i; j) such that 1 ≤ i < j ≤ N
and A[i] > A[j], and the number of local inversions is the number of
integers i such that 1 ≤ i < N and A[i] > A[i+1].

The Little Elephant has several such permutations. Help him to find
for each permutation whether it is good or not. Print YES for a
corresponding test case if it is good and NO otherwise.
//ANANDHA KRISHNAN.R
#include<stdio.h>
int main()
{int t,n,a[100],c,cc,i,j;
scanf("%d",&t);
while(t--)
{c=0;cc=0;
scanf("%d%d",&n,&a[0]);
if(n==1)
printf("YES\n");
else
{for(i=1;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i-1]>a[i])
cc++;
}
for(i=0;i<n-1;i++)
{for(j=i+1;j<n;j++)
{if(a[i]>a[j])
c++;
}
}
if(c==cc)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
APPROACH:I am taking t as the number of test cases and then
iterating the while loop.

You might also like