Friday, September 27, 2024
Google search engine
HomeData Modelling & AIMinimum p = p] operations to regain the given Array

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

Given an array, A[] (1 ā€“ indexed) of size ā€˜Nā€™ which contains a permutation of [1, N], the task is to find the minimum number of operations to be applied on any array P[] to get back the original array P[]. The operation must be applied at least once. In each operation, for every index of P[] we set P[i] = P[A[i]].

Examples:

Input: A[] = {1, 3, 2}
Output: 2
Explanation: 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 original array is reached.Ā 

Input: A[] = {5, 4, 2, 3, 1}
Output: 6
Explanation: 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 original array is reached.

Naive Approach:

A naive approach is to make any array and apply the given operation until the original array is reached again.

Below is the implementation of the approach.

C++




// C++ code for the naive approach
#include <bits/stdc++.h>
using namespace std;
Ā 
// comparing 2 arrays
bool checkEqual(vector<int>& A, vector<int>& B)
{
Ā Ā for (int i = 0; i < A.size(); i++) {
Ā Ā Ā Ā if (A[i] != B[i])
Ā Ā Ā Ā Ā Ā return false;
Ā Ā }
Ā Ā return true;
}
Ā 
// function to find minm operations
int minOperations(vector<int>& A)
{
Ā Ā int N = A.size();
Ā Ā vector<int> P(N, 0);
Ā Ā vector<int> originalArray(N, 0);
Ā 
Ā Ā // Let P be same as A
Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā P[i] = A[i];
Ā Ā Ā Ā originalArray[i] = P[i];
Ā Ā }
Ā 
Ā Ā // after applying operation 1 time
Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā P[i] = A[A[i] - 1];
Ā Ā }
Ā 
Ā Ā int operations = 1;
Ā Ā while (!checkEqual(originalArray, P)) {
Ā Ā Ā Ā vector<int> temp(N);
Ā Ā Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā temp[i] = P[A[i] - 1];
Ā Ā Ā Ā }
Ā Ā Ā Ā P = temp;
Ā Ā Ā Ā operations++;
Ā Ā }
Ā Ā return operations;
}
Ā 
int main()
{
Ā 
Ā Ā // Given input
Ā Ā vector<int> A = { 5, 4, 2, 3, 1 };
Ā 
Ā Ā // Function call
Ā Ā cout << minOperations(A);
Ā 
Ā Ā return 0;
}
Ā 
// This code is contributed by rakeshsahni


Java




// JAVA code for the naive approach
Ā 
import java.util.*;
Ā 
public class GFG {
Ā Ā Ā Ā // comparing 2 arrays
Ā Ā Ā Ā static boolean checkEqual(int[] A, int[] B)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < A.length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[i] != B[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // function to find minm operations
Ā Ā Ā Ā static int minOperations(int[] A)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int N = A.length;
Ā Ā Ā Ā Ā Ā Ā Ā int[] P = new int[N];
Ā Ā Ā Ā Ā Ā Ā Ā int[] originalArray = new int[N];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Let P be same as A
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā P[i] = A[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā originalArray[i] = P[i];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // after applying operation 1 time
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā P[i] = A[A[i] - 1];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int operations = 1;
Ā Ā Ā Ā Ā Ā Ā Ā while (!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 main(String[] args)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Given input
Ā Ā Ā Ā Ā Ā Ā Ā int[] A = { 5, 4, 2, 3, 1 };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function call
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(minOperations(A));
Ā Ā Ā Ā }
}


Python3




# Python code for the above approach
Ā 
# comparing 2 arrays
def checkEqual(A, B):
Ā Ā Ā Ā for i in range(len(A)):
Ā Ā Ā Ā Ā Ā Ā Ā if(A[i] is not B[i]):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return False
Ā 
Ā Ā Ā Ā return True
Ā 
# Function to find minimum operations
def minOperations(A):
Ā Ā Ā Ā N = len(A)
Ā Ā Ā Ā P = [0] * N
Ā Ā Ā Ā originalArray = [0] * N
Ā 
Ā Ā Ā Ā # let P be same as A
Ā Ā Ā Ā for i in range(N):
Ā Ā Ā Ā Ā Ā Ā Ā P[i] = A[i]
Ā Ā Ā Ā Ā Ā Ā Ā originalArray[i] = P[i]
Ā 
Ā Ā Ā Ā # after applying operation 1 time
Ā Ā Ā Ā for i in range(N):
Ā Ā Ā Ā Ā Ā Ā Ā P[i] = A[A[i]-1]
Ā 
Ā Ā Ā Ā operations = 1
Ā Ā Ā Ā while(checkEqual(originalArray, P) is not True):
Ā Ā Ā Ā Ā Ā Ā Ā temp = [0] * N
Ā Ā Ā Ā Ā Ā Ā Ā for i in range(N):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp[i] = P[A[i]-1]
Ā Ā Ā Ā Ā Ā Ā Ā P = temp
Ā Ā Ā Ā Ā Ā Ā Ā operations += 1
Ā 
Ā Ā Ā Ā return operations
Ā 
Ā 
A = [5, 4, 2, 3, 1]
Ā 
# Function call
print(minOperations(A))
Ā 
# This code is contributed by lokesh.


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
Ā 
class GFG
{
Ā Ā // comparing 2 arrays
Ā Ā static bool checkEqual(int[] A, int[] B)
Ā Ā {
Ā Ā Ā Ā for (int i = 0; i < A.Length; i++) {
Ā Ā Ā Ā Ā Ā if (A[i] != B[i])
Ā Ā Ā Ā Ā Ā Ā Ā return false;
Ā Ā Ā Ā }
Ā Ā Ā Ā return true;
Ā Ā }
Ā 
Ā Ā // function to find minm operations
Ā Ā static int minOperations(int[] A)
Ā Ā {
Ā Ā Ā Ā int N = A.Length;
Ā Ā Ā Ā int[] P = new int[N];
Ā Ā Ā Ā int[] originalArray = new int[N];
Ā 
Ā Ā Ā Ā // Let P be same as A
Ā Ā Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā P[i] = A[i];
Ā Ā Ā Ā Ā Ā originalArray[i] = P[i];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // after applying operation 1 time
Ā Ā Ā Ā for (int i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā P[i] = A[A[i] - 1];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā int operations = 1;
Ā Ā Ā Ā while (!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;
Ā Ā }
Ā 
Ā Ā // Driver code
Ā Ā public static void Main(String[] args)
Ā Ā {
Ā 
Ā Ā Ā Ā // Given input
Ā Ā Ā Ā int[] A = { 5, 4, 2, 3, 1 };
Ā 
Ā Ā Ā Ā // Function call
Ā Ā Ā Ā Console.WriteLine(minOperations(A));
Ā Ā }
}
Ā 
// This code is contributed by code_hunt.


Javascript




<script>
// JS code to implement the approach
Ā 
Ā Ā Ā Ā // comparing 2 arrays
Ā Ā Ā Ā function checkEqual(A, B)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < A.length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[i] != B[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return true;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // function to find minm operations
Ā Ā Ā Ā function minOperations(A)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā let N = A.length;
Ā Ā Ā Ā Ā Ā Ā Ā let P = new Array(N);
Ā Ā Ā Ā Ā Ā Ā Ā let originalArray = new Array(N);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Let P be same as A
Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā P[i] = A[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā originalArray[i] = P[i];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // after applying operation 1 time
Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā P[i] = A[A[i] - 1];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā let operations = 1;
Ā Ā Ā Ā Ā Ā Ā Ā while (!checkEqual(originalArray, P)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let temp = new Array(N);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp[i] = P[A[i] - 1];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā P = temp;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā operations++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return operations;
Ā Ā Ā Ā }
Ā 
// Driver code
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Given input
Ā Ā Ā Ā Ā Ā Ā Ā let A = [ 5, 4, 2, 3, 1 ];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function call
Ā Ā Ā Ā Ā Ā Ā Ā document.write(minOperations(A));
Ā 
// This code is contributed by sanjoy_62.
</script>


Output

6

Time Complexity: O(N * minOperations), for executing the operations until the original array is retrieved.
Auxiliary Space: O(N), for creating an additional array of size P.

Efficient Approach:

Use the following idea to solve the problem:

It can be observed that the elements form a cycle. When all the cycles are completed at the same operation for the first time that many moves are required.

Each cycle is completed after making moves same as their length. So all the cycles are completed at the same operation for the first time when LCM(all cycle lengths) number of moves are made.

Follow the below steps to solve the problem:

  • Declare an array ā€˜cycleLengths[]ā€™ to store the length of all cycles present.
  • Declare a Boolean array ā€˜visited[]ā€™ to check if the cycle length of corresponding element has already been calculated or not.
  • For every unvisited index
    • traverse all the elements of corresponding cycle while updating the ā€˜visited[]ā€™ and store its length in ā€˜cycleLength[]ā€™.
  • Return the LCM of all numbers present in ā€˜cycleLength[]ā€™.

Code implementation of above approach:

C++




// C++ code to implement the approach
Ā 
#include <iostream>
#include <vector>
using namespace std;
Ā 
// Function to calculate GCD of two numbers
int gcd(int a, int b)
{
Ā Ā Ā Ā if (a == 0)
Ā Ā Ā Ā Ā Ā Ā Ā return b;
Ā Ā Ā Ā return gcd(b % a, a);
}
Ā 
// Function to calculate LCM of two numbers
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
Ā 
// Traversing the cycle and returning
// the length of the cycle
int traverseCycle(int root, int A[], vector<int>& visited)
{
Ā Ā Ā Ā if (visited[root])
Ā Ā Ā Ā Ā Ā Ā Ā return 0;
Ā Ā Ā Ā visited[root] = true;
Ā Ā Ā Ā return 1 + traverseCycle(A[root - 1], A, visited);
}
Ā 
// Function to find minm operations
int minOperations(int A[], int N)
{
Ā 
Ā Ā Ā Ā vector<int> cycleLength;
Ā Ā Ā Ā vector<int> visited(N + 1, 0);
Ā 
Ā Ā Ā Ā // Detecting all cycles and storing
Ā Ā Ā Ā // their length in cycleLength List
Ā Ā Ā Ā for (int i = 1; i <= N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (!visited[i]) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int len = traverseCycle(i, A, visited);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cycleLength.push_back(len);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Finding lcm of all cycle lengths
Ā Ā Ā Ā int res = 1;
Ā Ā Ā Ā for (auto cycleLen : cycleLength) {
Ā Ā Ā Ā Ā Ā Ā Ā res = lcm(res, cycleLen);
Ā Ā Ā Ā }
Ā Ā Ā Ā return res;
}
Ā 
// Driver code
int main()
{
Ā Ā Ā Ā int A[] = { 5, 4, 2, 3, 1 };
Ā 
Ā Ā Ā Ā // Function call
Ā Ā Ā Ā cout << (minOperations(A, 5)) << endl;
Ā Ā Ā Ā ;
}
Ā 
// This code is contributed by garg28harsh


Java




// Java code to implement the approach
Ā 
import java.util.*;
Ā 
public class GFG {
Ā 
Ā Ā Ā Ā // Function to calculate GCD of two numbers
Ā Ā Ā Ā static int gcd(int a, int b)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (a == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return b;
Ā Ā Ā Ā Ā Ā Ā Ā return gcd(b % a, a);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to calculate LCM of two numbers
Ā Ā Ā Ā static int lcm(int a, int b)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return (a * b) / gcd(a, b);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Traversing the cycle and returning
Ā Ā Ā Ā // the length of the cycle
Ā Ā Ā Ā 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);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to find minm operations
Ā Ā Ā Ā static int minOperations(int[] A)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int N = A.length;
Ā Ā Ā Ā Ā Ā Ā Ā ArrayList<Integer> cycleLength = new ArrayList<>();
Ā Ā Ā Ā Ā Ā Ā Ā boolean[] visited = new boolean[N + 1];
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Detecting all cycles and storing
Ā Ā Ā Ā Ā Ā Ā Ā // their length in cycleLength List
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (!visited[i]) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int len = traverseCycle(i, A, visited);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cycleLength.add(len);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Finding lcm of all cycle lengths
Ā Ā Ā Ā Ā Ā Ā Ā int res = 1;
Ā Ā Ā Ā Ā Ā Ā Ā for (Integer cycleLen : cycleLength) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā res = lcm(res, cycleLen);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return res;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver code
Ā Ā Ā Ā public static void main(String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int[] A = { 5, 4, 2, 3, 1 };
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Function call
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(minOperations(A));
Ā Ā Ā Ā }
}


Python3




class GFG:
Ā Ā Ā Ā # Function to calculate GCD of two numbers
Ā Ā Ā Ā @staticmethod
Ā Ā Ā Ā def gcd(a,Ā  b):
Ā Ā Ā Ā Ā Ā Ā Ā if (a == 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return b
Ā Ā Ā Ā Ā Ā Ā Ā return GFG.gcd(b % a, a)
Ā Ā Ā Ā # Function to calculate LCM of two numbers
Ā 
Ā Ā Ā Ā @staticmethod
Ā Ā Ā Ā def lcm(a,Ā  b):
Ā Ā Ā Ā Ā Ā Ā Ā return int((a * b) / GFG.gcd(a, b))
Ā Ā Ā Ā # Traversing the cycle and returning
Ā Ā Ā Ā # the length of the cycle
Ā 
Ā Ā Ā Ā @staticmethod
Ā Ā Ā Ā def traverseCycle(root,Ā  A,Ā  visited):
Ā Ā Ā Ā Ā Ā Ā Ā if (visited[root]):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0
Ā Ā Ā Ā Ā Ā Ā Ā visited[root] = True
Ā Ā Ā Ā Ā Ā Ā Ā return 1 + GFG.traverseCycle(A[root - 1], A, visited)
Ā Ā Ā Ā # Function to find minm operations
Ā 
Ā Ā Ā Ā @staticmethod
Ā Ā Ā Ā def minOperations(A):
Ā Ā Ā Ā Ā Ā Ā Ā N = 5
Ā Ā Ā Ā Ā Ā Ā Ā cycleLength = []
Ā Ā Ā Ā Ā Ā Ā Ā visited = [False] * (N + 1)
Ā Ā Ā Ā Ā Ā Ā Ā # Detecting all cycles and storing
Ā Ā Ā Ā Ā Ā Ā Ā # their length in cycleLength List
Ā Ā Ā Ā Ā Ā Ā Ā i = 1
Ā Ā Ā Ā Ā Ā Ā Ā while (i <= N):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (not visited[i]):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā len = GFG.traverseCycle(i, A, visited)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cycleLength.append(len)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i += 1
Ā Ā Ā Ā Ā Ā Ā Ā # Finding lcm of all cycle lengths
Ā Ā Ā Ā Ā Ā Ā Ā res = 1
Ā Ā Ā Ā Ā Ā Ā Ā for cycleLen in cycleLength:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā res = GFG.lcm(res, cycleLen)
Ā Ā Ā Ā Ā Ā Ā Ā return res
Ā Ā Ā Ā # Driver code
Ā 
Ā Ā Ā Ā @staticmethod
Ā Ā Ā Ā def main(args):
Ā Ā Ā Ā Ā Ā Ā Ā A = [5, 4, 2, 3, 1]
Ā Ā Ā Ā Ā Ā Ā Ā # Function call
Ā Ā Ā Ā Ā Ā Ā Ā print(GFG.minOperations(A))
Ā 
Ā 
if __name__ == "__main__":
Ā Ā Ā Ā GFG.main([])


C#




// C# code to implement the approach
using System;
using System.Collections.Generic;
Ā 
public class GFG
{
Ā 
Ā Ā // Function to calculate GCD of two numbers
Ā Ā static int gcd(int a, int b)
Ā Ā {
Ā Ā Ā Ā if (a == 0)
Ā Ā Ā Ā Ā Ā return b;
Ā Ā Ā Ā return gcd(b % a, a);
Ā Ā }
Ā 
Ā Ā // Function to calculate LCM of two numbers
Ā Ā static int lcm(int a, int b)
Ā Ā {
Ā Ā Ā Ā return (a * b) / gcd(a, b);
Ā Ā }
Ā 
Ā Ā // Traversing the cycle and returning
Ā Ā // the length of the cycle
Ā Ā 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);
Ā Ā }
Ā 
Ā Ā // Function to find minm operations
Ā Ā static int minOperations(int[] A)
Ā Ā {
Ā Ā Ā Ā int N = A.Length;
Ā Ā Ā Ā List<int> cycleLength = new List<int>();
Ā Ā Ā Ā Boolean[] visited = new Boolean[N + 1];
Ā 
Ā Ā Ā Ā // Detecting all cycles and storing
Ā Ā Ā Ā // their length in cycleLength List
Ā Ā Ā Ā for (int i = 1; i <= N; i++)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā if (!visited[i])
Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int len = traverseCycle(i, A, visited);
Ā Ā Ā Ā Ā Ā Ā Ā cycleLength.Add(len);
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Finding lcm of all cycle lengths
Ā Ā Ā Ā int res = 1;
Ā Ā Ā Ā foreach (int cycleLen in cycleLength)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā res = lcm(res, cycleLen);
Ā Ā Ā Ā }
Ā Ā Ā Ā return res;
Ā Ā }
Ā 
Ā Ā // Driver code
Ā Ā public static void Main()
Ā Ā {
Ā Ā Ā Ā int[] A = { 5, 4, 2, 3, 1 };
Ā 
Ā Ā Ā Ā // Function call
Ā Ā Ā Ā Console.Write(minOperations(A));
Ā Ā }
}
Ā 
// This code is contributed by saurabh_jaiswal.


Javascript




// Javascript code to implement the approach
Ā 
Ā Ā Ā Ā // Function to calculate GCD of two numbers
Ā Ā Ā Ā function gcd(a, b)
{
Ā Ā Ā Ā if (a == 0)
Ā Ā Ā Ā Ā Ā Ā Ā return b;
Ā Ā Ā Ā return gcd(b % a, a);
}
Ā 
// Function to calculate LCM of two numbers
function lcm(a, b)
{
Ā Ā Ā Ā let c = a * b;
Ā Ā Ā Ā return (c / gcd(a, b));
}
Ā 
// Traversing the cycle and returning
// the length of the cycle
function traverseCycle(root, A, visited)
{
Ā Ā Ā Ā if (visited[root])
Ā Ā Ā Ā Ā Ā Ā Ā return 0;
Ā Ā Ā Ā visited[root] = true;
Ā Ā Ā Ā return 1 + traverseCycle(A[root - 1], A, visited);
}
Ā 
// Function to find minm operations
function minOperations(A, N)
{
Ā 
Ā Ā Ā Ā let cycleLength = [];
Ā 
Ā Ā Ā Ā let visited = [];
Ā Ā Ā Ā for (let i = 0; i <= N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā visited.push(0);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Detecting all cycles and storing
Ā Ā Ā Ā // their length in cycleLength List
Ā Ā Ā Ā for (let i = 1; i <= N; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (visited[i] == false) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let len = traverseCycle(i, A, visited);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cycleLength.push(len);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Finding lcm of all cycle lengths
Ā Ā Ā Ā let res = 1;
Ā Ā Ā Ā for (let i = 0; i < cycleLength.length; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā res = lcm(res, cycleLength[i]);
Ā Ā Ā Ā }
Ā Ā Ā Ā return res;
}
Ā 
// Driver code
let A = [ 5, 4, 2, 3, 1 ];
Ā 
// Function call
console.log(minOperations(A, 5));
Ā 
// This code is contributed by garg28harsh.


Output

6

Time Complexity: O(N*log(Arr[i])), where N is the size of the given array.
Auxiliary Space: O(N), for creating an additional array of size N + 1.

Feeling lost in the world of random DSA topics, wasting time without progress? Itā€™s time for a change! Join our DSA course, where weā€™ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfGā€™s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
01 Nov, 2022
Like Article
Save Article


Previous

<!ā€“

8 Min Readā€‚|ā€‚Java

ā€“>


Next


<!ā€“

8 Min Readā€‚|ā€‚Java

ā€“>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?