Number of shuffles required for each element to return to its initial position
Last Updated :
10 Oct, 2022
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 2
Explanation:
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(N2).
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++
#include <bits/stdc++.h>
using namespace std;
void countShuffles( int * A, int N)
{
int count[N];
bool vis[N];
memset (vis, false , sizeof (vis));
for ( int i = 0; i < N; i++)
A[i]--;
for ( int i = 0; i < N; i++) {
if (!vis[i]) {
vector< int > cur;
int pos = i;
while (!vis[pos]) {
cur.push_back(pos);
vis[pos] = true ;
pos = A[pos];
}
for ( auto el : cur)
count[el] = cur.size();
}
}
for ( int i = 0; i < N; i++)
cout << count[i] << " " ;
cout << endl;
}
int main()
{
int arr[] = { 4, 6, 2, 1, 5, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
countShuffles(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void countShuffles( int [] A, int N)
{
int [] count = new int [N];
boolean [] vis = new boolean [N];
for ( int i = 0 ; i < N; i++)
A[i]--;
for ( int i = 0 ; i < N; i++)
{
if (!vis[i])
{
Vector<Integer> cur = new Vector<>();
int pos = i;
while (!vis[pos])
{
cur.add(pos);
vis[pos] = true ;
pos = A[pos];
}
for ( int k = 0 ; k < cur.size(); k++)
count[cur.get(k)] = cur.size();
}
}
for ( int k = 0 ; k < N; k++)
System.out.print(count[k] + " " );
}
public static void main(String[] args)
{
int arr[] = { 4 , 6 , 2 , 1 , 5 , 3 };
int N = arr.length;
countShuffles(arr, N);
}
}
|
Python3
def countShuffles(A, N):
count = [ 0 ] * N
vis = [ False ] * N
for i in range (N):
A[i] - = 1
for i in range (N):
if ( not vis[i]):
cur = []
pos = i
while ( not vis[pos]):
cur.append(pos)
vis[pos] = True
pos = A[pos]
for el in cur:
count[el] = len (cur)
for i in range (N):
print (count[i], end = " " )
print ()
if __name__ = = "__main__" :
arr = [ 4 , 6 , 2 , 1 , 5 , 3 ]
N = len (arr)
countShuffles(arr, N)
|
C#
using System;
using System.Collections;
class GFG{
static void countShuffles( int [] A, int N)
{
int [] count = new int [N];
bool [] vis = new bool [N];
for ( int i = 0; i < N; i++)
A[i]--;
for ( int i = 0; i < N; i++)
{
if (!vis[i])
{
ArrayList cur = new ArrayList();
int pos = i;
while (!vis[pos])
{
cur.Add(pos);
vis[pos] = true ;
pos = A[pos];
}
for ( int k = 0; k < cur.Count; k++)
count[( int )cur[k]] = cur.Count;
}
}
for ( int k = 0; k < N; k++)
Console.Write(count[k] + " " );
}
public static void Main( string [] args)
{
int []arr = { 4, 6, 2, 1, 5, 3 };
int N = arr.Length;
countShuffles(arr, N);
}
}
|
Javascript
<script>
function countShuffles(A, N)
{
var count = Array(N);
var vis = Array(N).fill( false );
for ( var i = 0; i < N; i++)
A[i]--;
for ( var i = 0; i < N; i++) {
if (!vis[i]) {
var cur = [];
var pos = i;
while (!vis[pos]) {
cur.push(pos);
vis[pos] = true ;
pos = A[pos];
}
for ( var e1 = 0; e1<cur.length; e1++)
{
count[cur[e1]]=cur.length;
}
}
}
for ( var i = 0; i < N; i++)
document.write( count[i] + " " );
document.write( "<br>" );
}
var arr = [ 4, 6, 2, 1, 5, 3 ];
var N = arr.length;
countShuffles(arr, N);
</script>
|
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...