Little Elephant and Permutations | CodeChef Solution

Hello coders, today we are going to solve Little Elephant and Permutations CodeChef Solution whose Problem Code is LEPERMUT.

Little Elephant and Permutations

Task

The Little Elephant likes permutations. This time he has a permutation A[1]A[2], …, A[N] of numbers 12, …, 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 (ij) 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.

Input Format

The first line of the input contains a single integer T, the number of test cases. T test cases follow. The first line of each test case contains a single integer N, the size of a permutation. The next line contains N space separated integers A[1]A[2], …, A[N].

Output Format

For each test case output a single line containing the answer for the corresponding test case. It should be YES if the corresponding permutation is good and NO otherwise.

Constraints

  • 1 ≤ T ≤ 474
  • 1 ≤ N ≤ 100
  • It is guaranteed that the sequence A[1]A[2], …, A[N] is a permutation of numbers 12, …, N.

Example

Sample Input

4
1
1
2
2 1
3
3 2 1
4
1 3 2 4

Sample Output

YES
YES
NO
YES

Explanation

Case 1. Here N = 1, so we have no pairs (i; j) with 1 ≤ i < j ≤ N. So the number of inversions is equal to zero. The number of local inversion is also equal to zero. Hence this permutation is good.

Case 2. Here N = 2, and we have one pair (i; j) with 1 ≤ i < j ≤ N, the pair (1; 2). Since A[1] = 2 and A[2] = 1 then A[1] > A[2] and the number of inversions is equal to 1. The number of local inversion is also equal to 1 since we have one value of i for which 1 ≤ i < N (the value i = 1) and A[i] > A[i+1] for this value of i since A[1] > A[2]. Hence this permutation is also good.

Case 3. Here N = 3, and we have three pairs (i; j) with 1 ≤ i < j ≤ N. We have A[1] = 3, A[2] = 2, A[3] = 1. Hence A[1] > A[2]A[1] > A[3] and A[2] > A[3]. So the number of inversions is equal to 3. To count the number of local inversion we should examine inequalities A[1] > A[2] and A[2] > A[3]. They both are satisfied in our case, so we have 2 local inversions. Since 2 ≠ 3 this permutations is not good.

Case 4. Here we have only one inversion and it comes from the pair (2; 3) since A[2] = 3 > 2 = A[3]. This pair gives also the only local inversion in this permutation. Hence the number of inversions equals to the number of local inversions and equals to one. So this permutation is good.

Solution – Little Elephant and Permutations

C++

#include <iostream>
using namespace std;

int main() 
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int A[n],j,i,count1=0,count2=0;
        for(i=0;i<n;i++)
        {
            cin>>A[i];
        }
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(A[i]>A[j])
                {
                    count1++;
                }
            }
        }
        for(i=0;i<n-1;i++)
        {
            if(A[i]>A[i+1])
            count2++;
            
        }
        if(count1!=count2)
        cout<<"NO"<<endl;
        else 
        cout<<"YES"<<endl;
    }
	return 0;
}

Python

for _ in range(int(input())):
    n = int(input())
    x = [int(x) for x in input().split()]
    count = 0
    flag = 0
    for i in range(n-1):
        
        if x[i] > x[i + 1] and i < n:
            count += 1
        
        for j in range(i+1, n):
            if x[i] > x[j]:
                flag += 1

    if count == flag:
        print('YES')
    else:
        print("NO")

Java

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int t;
		t=in.nextInt();
		while(t!=0){
			int n;
			n=in.nextInt();
			int[] a=new int[n];
			for (int i=0;i<n ;i++ ) {
				a[i]=in.nextInt();
			}
			int inv=0,local=0;
			for (int i=0;i<n-1 ;i++ ) {
				for (int j=i+1;j<n ;j++ ) {
					if (a[i]>a[j]) {
						inv++;
					}
				}
				if (a[i]>a[i+1]) {
					local++;
				}
			}
			if (inv==local) {
				System.out.println("YES");
			}
			else{
				System.out.println("NO");
			}
			t--;
		}
	}
}

Disclaimer: The above Problem (Little Elephant and Permutations) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *