# Minimum time to return array to its original state after given modifications

Given two arrays of integers *arr* and *P* such that after a cycle an element *arr[i]* will be at location *arr[P[i]]*. The task is to find the minimum number of cycles after that all the elements of the array have returned to their original locations.

**Examples:**

Input:arr[] = {1, 2, 3}, P[] = {3, 2, 1}Output:2 After first move array will be {3, 2, 1} after second move array will be {1, 2, 3}Input:arr[] = {4, 5, 1, 2, 3}, P[] = {1, 4, 2, 5, 3}Output:4

**Approach:** This problem seems to be a typical mathematical problem but if we carefully observe it then we will find that we have to find only the permutation cycles i.e. connected components (formed by the cycles from element movements) and number of nodes in each connected component will represent the time in which the integers will be back in it’s original position for that particular cycle.

For overall graph, do the LCM of the count of nodes from every connected component which is the answer.

Below is the implementation of above approach:

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return ` `// lcm of two numbers ` `int` `lcm(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `(a * b) / (__gcd(a, b)); ` `} ` ` ` `int` `dfs(` `int` `src, vector<` `int` `> adj[], vector<` `bool` `> &visited) ` `{ ` ` ` `visited[src] = ` `true` `; ` ` ` `int` `count = 1; ` ` ` ` ` `for` `(` `int` `i = 0; i < adj[src].size(); i++) ` ` ` `if` `(!visited[adj[src][i]]) ` ` ` `count += dfs(adj[src][i], adj, visited); ` ` ` ` ` `return` `count; ` `} ` ` ` `int` `findMinTime(` `int` `arr[], ` `int` `P[], ` `int` `n) ` `{ ` ` ` `// Make a graph ` ` ` `vector<` `int` `> adj[n+1]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Add edge ` ` ` `adj[arr[i]].push_back(P[i]); ` ` ` `} ` ` ` ` ` `// Count reachable nodes from every node. ` ` ` `vector<` `bool` `> visited(n+1); ` ` ` `int` `ans = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(!visited[i]) { ` ` ` `ans = lcm(ans, dfs(i, adj, visited)); ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 1, 2, 3 }; ` ` ` `int` `P[] = { 3, 2, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << findMinTime(arr, P, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Find the probability of a state at a given time in a Markov chain | Set 1
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Find original array from encrypted array (An array of sums of other elements)
- Generate original array from an array that store the counts of greater elements on right
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Minimum time to reach a point with +t and -t moves at time t
- Build original array from the given sub-sequences
- Generate original array from difference between every two consecutive elements
- Print n smallest elements from given array in their original order
- Count subarrays having total distinct elements same as original array
- Minimum time required to rot all oranges
- Maximum removal from array when removal time >= waiting time
- Minimum time required to complete a work by N persons together
- Minimum time required to fill a cistern using N pipes
- Minimum time required to transport all the boxes from source to the destination under the given constraints

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.