Given an integer array **arr[]** containing a permutation of integers from **1** to **N**. Let **K[]** be some arbitrary array. Every element in the array arr[i] represents the index of the element to which the element which is initially at the position ‘i’ in the array K[] is placed. The task is to find the number of shuffles needed to be performed on K[] to get back the elements to the initial position.

**Note:** It is given that at least 1 shuffle has to be made over **K[]** (i.e.), the element which is initially at the position ‘i’ in the array **K[]** has to be placed in the index **arr[i]** at least once for all the elements in the array K[].

**Examples:**

Input:arr[] = {3, 4, 1, 2}Output:2 2 2 2Explanation:

Let the initial array B[] = {5, 6, 7, 8}. Therefore:

After 1st shuffle: The first element will go to the third position, the second element will go to the fourth position. Therefore, B[] = {7, 8, 5, 6}.

After 2nd shuffle: The first element will go to the third position, the second element will go to the fourth position. Therefore, B[] = {5, 6, 7, 8}.

Therefore, the number of shuffles is 2 for all the elements to get back to the same initial position.

Input :arr[] = {4, 6, 2, 1, 5, 3}Output :2 3 3 2 1 3

**Naive Approach:** The naive approach for this problem is to count the number of shuffles needed for every element. The time complexity for this approach would be **O(N ^{2})**.

**Efficient Approach:** An efficient approach for this problem is to first calculate the number of cycles in the problem. The elements in the same cycle have the same number of shuffles. For example, in the given array arr[] = {3, 4, 1, 2}:

- At every shuffle, the first element always goes to the third place and the third element always comes to the first place.
- Therefore, it can be concluded that both the elements are in a cycle. Therefore, the number of shuffles taken for both the elements is 2 irrespective of what the array
**K[]**is. - Therefore, the idea is to find the number of such cycles in the array and skip those elements.

Below is the implementation of the above approach:

## C++

`// C++ program to find the number of` `// shuffles required for each element` `// to return to its initial position` ` ` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Function to count the number of` `// shuffles required for each element` `// to return to its initial position` `void` `countShuffles(` `int` `* A, ` `int` `N)` `{` ` ` `// Initialize array to store` ` ` `// the counts` ` ` `int` `count[N];` ` ` ` ` `// Initialize visited array to` ` ` `// check if visited before or not` ` ` `bool` `vis[N];` ` ` `memset` `(vis, ` `false` `, ` `sizeof` `(vis));` ` ` ` ` `// Making the array 0-indexed` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `A[i]--;` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `if` `(!vis[i]) {` ` ` ` ` `// Initialize vector to store the` ` ` `// elements in the same cycle` ` ` `vector<` `int` `> cur;` ` ` ` ` `// Initialize variable to store` ` ` `// the current element` ` ` `int` `pos = i;` ` ` ` ` `// Count number of shuffles` ` ` `// for current element` ` ` `while` `(!vis[pos]) {` ` ` ` ` `// Store the elements in` ` ` `// the same cycle` ` ` `cur.push_back(pos);` ` ` ` ` `// Mark visited` ` ` `vis[pos] = ` `true` `;` ` ` ` ` `// Make the shuffle` ` ` `pos = A[pos];` ` ` `}` ` ` ` ` `// Store the result for all the` ` ` `// elements in the same cycle` ` ` `for` `(` `auto` `el : cur)` ` ` `count[el] = cur.size();` ` ` `}` ` ` `}` ` ` ` ` `// Print the result` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `cout << count[i] << ` `" "` `;` ` ` `cout << endl;` `}` ` ` `// Driver code` `int` `main()` `{` ` ` `int` `arr[] = { 4, 6, 2, 1, 5, 3 };` ` ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` ` ` `countShuffles(arr, N);` ` ` ` ` `return` `0;` `}` |

## Java

`// Java program to find the number of` `// shuffles required for each element` `// to return to its initial position` `import` `java.util.*;` ` ` `class` `GFG {` ` ` `// Function to count the number of` `// shuffles required for each element` `// to return to its initial position` `static` `void` `countShuffles(` `int` `[] A, ` `int` `N)` `{` ` ` ` ` `// Initialize array to store` ` ` `// the counts` ` ` `int` `[] count = ` `new` `int` `[N];` ` ` ` ` `// Initialize visited array to` ` ` `// check if visited before or not` ` ` `boolean` `[] vis = ` `new` `boolean` `[N];` ` ` ` ` `// Making the array 0-indexed` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `A[i]--;` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `{` ` ` `if` `(!vis[i])` ` ` `{` ` ` ` ` `// Initialize vector to store the` ` ` `// elements in the same cycle` ` ` `Vector<Integer> cur = ` `new` `Vector<>(); ` ` ` ` ` `// Initialize variable to store` ` ` `// the current element` ` ` `int` `pos = i;` ` ` ` ` `// Count number of shuffles` ` ` `// for current element` ` ` `while` `(!vis[pos])` ` ` `{` ` ` ` ` `// Store the elements in` ` ` `// the same cycle` ` ` `cur.add(pos);` ` ` ` ` `// Mark visited` ` ` `vis[pos] = ` `true` `;` ` ` ` ` `// Make the shuffle` ` ` `pos = A[pos];` ` ` `}` ` ` ` ` `// Store the result for all the` ` ` `// elements in the same cycle` ` ` `for` `(` `int` `k = ` `0` `; k < cur.size(); k++)` ` ` `count[cur.get(k)] = cur.size();` ` ` `}` ` ` `}` ` ` ` ` `// Print the result` ` ` `for` `(` `int` `k = ` `0` `; k < N; k++)` ` ` `System.out.print(count[k] + ` `" "` `);` `}` ` ` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `arr[] = { ` `4` `, ` `6` `, ` `2` `, ` `1` `, ` `5` `, ` `3` `};` ` ` `int` `N = arr.length;` ` ` ` ` `countShuffles(arr, N);` `}` `}` ` ` `// This code is contributed by offbeat` |

## Python3

`# Python3 program to find the number of` `# shuffles required for each element` `# to return to its initial position` ` ` `# Function to count the number of` `# shuffles required for each element` `# to return to its initial position` `def` `countShuffles(A, N):` ` ` ` ` `# Initialize array to store` ` ` `# the counts` ` ` `count ` `=` `[` `0` `] ` `*` `N` ` ` ` ` `# Initialize visited array to` ` ` `# check if visited before or not` ` ` `vis ` `=` `[` `False` `] ` `*` `N` ` ` ` ` `# Making the array 0-indexed` ` ` `for` `i ` `in` `range` `(N):` ` ` `A[i] ` `-` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(N):` ` ` `if` `(` `not` `vis[i]):` ` ` ` ` `# Initialize vector to store the` ` ` `# elements in the same cycle` ` ` `cur ` `=` `[]` ` ` ` ` `# Initialize variable to store` ` ` `# the current element` ` ` `pos ` `=` `i` ` ` ` ` `# Count number of shuffles` ` ` `# for current element` ` ` `while` `(` `not` `vis[pos]):` ` ` ` ` `# Store the elements in` ` ` `# the same cycle` ` ` `cur.append(pos)` ` ` ` ` `# Mark visited` ` ` `vis[pos] ` `=` `True` ` ` ` ` `# Make the shuffle` ` ` `pos ` `=` `A[pos]` ` ` ` ` `# Store the result for all the` ` ` `# elements in the same cycle` ` ` `for` `el ` `in` `cur:` ` ` `count[el] ` `=` `len` `(cur)` ` ` ` ` `# Print the result` ` ` `for` `i ` `in` `range` `(N):` ` ` `print` `(count[i], end ` `=` `" "` `)` ` ` `print` `()` ` ` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `arr ` `=` `[ ` `4` `, ` `6` `, ` `2` `, ` `1` `, ` `5` `, ` `3` `]` ` ` `N ` `=` `len` `(arr)` ` ` `countShuffles(arr, N)` ` ` `# This code is contributed by chitranayal` |

## C#

`// C# program to find the number of` `// shuffles required for each element` `// to return to its initial position` `using` `System;` `using` `System.Collections;` ` ` `class` `GFG{` ` ` `// Function to count the number of` `// shuffles required for each element` `// to return to its initial position` `static` `void` `countShuffles(` `int` `[] A, ` `int` `N)` `{` ` ` ` ` `// Initialize array to store` ` ` `// the counts` ` ` `int` `[] count = ` `new` `int` `[N];` ` ` ` ` `// Initialize visited array to` ` ` `// check if visited before or not` ` ` `bool` `[] vis = ` `new` `bool` `[N];` ` ` ` ` `// Making the array 0-indexed` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `A[i]--;` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` `if` `(!vis[i])` ` ` `{` ` ` ` ` `// Initialize vector to store the` ` ` `// elements in the same cycle` ` ` `ArrayList cur = ` `new` `ArrayList(); ` ` ` ` ` `// Initialize variable to store` ` ` `// the current element` ` ` `int` `pos = i;` ` ` ` ` `// Count number of shuffles` ` ` `// for current element` ` ` `while` `(!vis[pos])` ` ` `{` ` ` ` ` `// Store the elements in` ` ` `// the same cycle` ` ` `cur.Add(pos);` ` ` ` ` `// Mark visited` ` ` `vis[pos] = ` `true` `;` ` ` ` ` `// Make the shuffle` ` ` `pos = A[pos];` ` ` `}` ` ` ` ` `// Store the result for all the` ` ` `// elements in the same cycle` ` ` `for` `(` `int` `k = 0; k < cur.Count; k++)` ` ` `count[(` `int` `)cur[k]] = cur.Count;` ` ` `}` ` ` `}` ` ` ` ` `// Print the result` ` ` `for` `(` `int` `k = 0; k < N; k++)` ` ` `Console.Write(count[k] + ` `" "` `);` `}` ` ` `// Driver code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` `int` `[]arr = { 4, 6, 2, 1, 5, 3 };` ` ` `int` `N = arr.Length;` ` ` ` ` `countShuffles(arr, N);` `}` `}` ` ` `// This code is contributed by rutvik_56` |

**Output:**

2 3 3 2 1 3

**Time Complexity:** *O(N)*, where N is the size of the array.