Minimal p[i] = p[arr[i]] operations to regain the given Array


Given an array, A[] (1 – listed) of dimension ‘N’ which incorporates a permutation of [1, N], the duty is to search out the minimal variety of operations to be utilized on any array P[] to get again the unique array P[]. The operation should be utilized no less than as soon as. In every operation, for each index of P[] we we set P[i] = P[A[i]].

Examples:

Enter: A[] = {1, 3, 2}
Output: 2
Rationalization: Let P[] = {7, 4, 2}. 
After 1 operation, {P[1], P[3], P[2]} = {1, 2, 4}.
After 2 operations, {P[1], P[3], P[2]} = {7, 4, 2}
After 2 operation unique array is reached. 

Enter: A[] = {5, 4, 2, 3, 1}
Output: 6
Rationalization: Let P = {1, 2, 3, 4, 5},  
After 1 operation {P[5], P[4], P[2], P[3], P[1]} = {5, 4, 2, 3, 1}
After 2 operation {P[5], P[4], P[2], P[3], P[1]} = {1, 3, 4, 2, 5}
After 3 operation {P[5], P[4], P[2], P[3], P[1]} = {5, 2, 3, 4, 1}
After 4 operation {P[5], P[4], P[2], P[3], P[1]} = {1, 4, 2, 3, 5}
After 5 operation {P[5], P[4], P[2], P[3], P[1]} = {5, 3, 4, 2, 1}
After 6 operation {P[5], P[4], P[2], P[3], P[1]} = {1, 2, 3, 4, 5}
After 6 operation unique array is reached.

Naive Strategy:

A naive method is to make any array and apply the given operation till the unique array is reached once more.

Beneath is the implementation of the method.

Java

  

import java.util.*;

  

public class GFG {

    

    static boolean checkEqual(int[] A, int[] B)

    {

        for (int i = 0; i < A.size; i++) {

            if (A[i] != B[i])

                return false;

        }

        return true;

    }

  

    

    static int minOperations(int[] A)

    {

        int N = A.size;

        int[] P = new int[N];

        int[] originalArray = new int[N];

  

        

        for (int i = 0; i < N; i++) {

            P[i] = A[i];

            originalArray[i] = P[i];

        }

  

        

        for (int i = 0; i < N; i++) {

            P[i] = A[A[i] - 1];

        }

  

        int operations = 1;

        whereas (!checkEqual(originalArray, P)) {

            int[] temp = new int[N];

            for (int i = 0; i < N; i++) {

                temp[i] = P[A[i] - 1];

            }

            P = temp;

            operations++;

        }

        return operations;

    }

    public static void fundamental(String[] args)

    {

  

        

        int[] A = { 5, 4, 2, 3, 1 };

  

        

        System.out.println(minOperations(A));

    }

}

Time Complexity: O(N * minOperations), for executing the operations till the unique array is retrieved.
Auxiliary House: O(N), for creating a further array of dimension P.

Environment friendly Strategy:

Use the next thought to unravel the issue:

It may be noticed that the weather kind a cycle. When all of the cycles are accomplished on the similar operation for the primary time that many strikes are required.

Every cycle is accomplished after making strikes similar as their size. So all of the cycles are accomplished on the similar operation for the primary time when LCM(all cycle lengths) variety of strikes are made.

Observe the under steps to unravel the issue:

  • Declare an array ‘cycleLengths[]’ to retailer the size of all cycles current.
  • Declare a Boolean array ‘visited[]’ to examine if the cycle size of corresponding factor has already been calculated or not.
  • For each unvisited index
    • traverse all the weather of corresponding cycle whereas updating the ‘visited[]’ and retailer its size in ‘cycleLength[]’.
  • Return the LCM of all numbers current in ‘cycleLength[]’.

Code implementation of above method:

Java

  

import java.util.*;

  

public class GFG {

  

    

    static int gcd(int a, int b)

    {

        if (a == 0)

            return b;

        return gcd(b % a, a);

    }

  

    

    static int lcm(int a, int b)

    {

        return (a * b) / gcd(a, b);

    }

  

    

    

    static int traverseCycle(int root, int[] A,

                             boolean[] visited)

    {

        if (visited[root])

            return 0;

        visited[root] = true;

        return 1 + traverseCycle(A[root - 1], A, visited);

    }

  

    

    static int minOperations(int[] A)

    {

        int N = A.size;

        ArrayList<Integer> cycleLength = new ArrayList<>();

        boolean[] visited = new boolean[N + 1];

  

        

        

        for (int i = 1; i <= N; i++) {

            if (!visited[i]) {

                int len = traverseCycle(i, A, visited);

                cycleLength.add(len);

            }

        }

  

        

        int res = 1;

        for (Integer cycleLen : cycleLength) {

            res = lcm(res, cycleLen);

        }

        return res;

    }

  

    

    public static void fundamental(String[] args)

    {

        int[] A = { 5, 4, 2, 3, 1 };

  

        

        System.out.println(minOperations(A));

    }

}

Time Complexity: O(N*log(Arr[i])), the place N is the scale of the given array.
Auxiliary House: O(N), for creating a further array of dimension N + 1.

Leave a Reply