简体   繁体   中英

Find the minimum number of operations after which P becomes an identity permutation

Consider a Permutation P of length n . Let us define an operation on P as follows:

P[i]=P[P[i]];     for every i from 1 to n 

for example:

P[n] = {2, 3, 1};

After 1 operation:

P[1] becomes P[P[1]] = P[2]= 3;
P[2] becomes P[P[2]] = P[3]= 1;
P[3] becomes P[P[3]] = P[1]= 2;

So, P becomes [3, 1, 2] ;

You need to find the minimum number of operations to be performed after which P becomes an Identity Permutation.

Output -1 if not possible.

Input 1:

P = [2 , 1];

Output 1:

1

As it will become Identity Permutation after 1 operation only.

Input 2:

P = [2 , 3 , 1];

Output 2:

-1

As Identity Permutation is not possible by the given operation.

I've tried to solve this code as follows, but I'm not getting the right way to solve it.

Java:

import java.util.Scanner;

public class Permutation {
    public static void main(String args[]) throws Exception {
        Scanner s = new Scanner(System. in );
        System.out.println("Enter the number of Test Cases");
        int t = s.nextInt();
        for (int i = 0; i < t; i++) {
            System.out.println("Enter the number n");
            int n = s.nextInt();
            int[] arr = new int[n + 1];
            for (i = 1; i <= n; i++) {
                arr[i] = s.nextInt();
            }
            int count = 0;
            int[] arr1 = new int[n + 1];``
            arr1 = arr;
            do {
                for (i = 1; i <= n; i++) {
                    arr1[i] = arr1[arr1[i]];
                }
                count++;
            }
            while ( arr != arr1 );
            System.out.println(count);
        }
    }
}

In this code,I loop until n! tries or when find the permutation.If check is true,then I found and can write the count,if count reaches the n! which means all permutations are done and I couldn't find the identity permutation and I write -1.Loop boundary is like that.Let's look at the algorithm.

I copied orijinal array to another array to don't lose any information.And after each iteration when array is changed,I also kept it.

After arr1[i] = arr[arr[i]]; ,I sorted the arr1 and check whether sorted arr1 is equal previous arr1 or not,if they are equals then I found the permutation,break the loop make check boolean true and print result,else iterate one more time.

import java.util.Arrays;
import java.util.Scanner;

public class Permutation {
  public static void main(String args[] ) throws Exception {
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the number of Test Cases");
    int t = s.nextInt();
    for(int i=0;i<t;i++)
    {
      System.out.println("Enter the number n");
      int n = s.nextInt();
      int[] arr=new int[n+1];
      for(i=1;i<=n;i++){
        arr[i]=s.nextInt();
      }
    int count=0;
    int[] arr1=new int[n+1];
    arr1 = Arrays.copyOf(arr, arr.length);
    boolean check;
    do {
        for (i = 1; i <= n; i++) {
            arr1[i] = arr[arr[i]];
        }
        arr = Arrays.copyOf(arr1, arr1.length);
        count++;
        int [] temp = Arrays.copyOf(arr1, arr1.length);
        Arrays.sort(arr1);
        check = false;
        for(int m =1;m<=n;m++){
            if(arr1[m]==temp[m])
                check = true;
            else{
                check = false;
                break;
            }
        }
    }
    while(count<fact(n) && check!=true);

    if(count == fact(n))
    System.out.println("-1");
    else
        System.out.println(count);
  }
}
public static int fact(int n){
  int temp = 1;
  for(int i=1;i<=n;i++)
    temp*=i;
  return temp;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM