Given an array arr[] consisitng of a permutation of first N natural numbers, the task is to find the minimum number of clockwise circular rotations of the array required to maximise the number of elements satisfying the condition arr[i] = i ( 1-based indexing ) where 1 ≤ i ≤ N.
Examples:
Input: arr[] = {4, 5, 1, 2, 3}
Output: 3
Explanation: Rotating the array thrice, the array modifies to {1, 2, 3, 4, 5}. All the array elements satisfy the condition arr[i] = i.Input: arr[] = {3, 4, 1, 5, 2}
Output: 2
Explanation: Rotating the array twice, the array modifies to {5, 2, 3, 4, 1}. Three array elements satisfy the condition arr[i] = i, which is the maximum possible for the given array.
Approach: Follow the steps below to solve the problem:
- Initialize two integers maxi and ans, and two arrays new_arr[] and freq[].
- Traverse the array arr[] an for each element, count the number of indices separating it from its correct position, i.e |(arr[i] – i + N) % N|.
- Store the counts for each array element in a new array new_arr[].
- Store the count of frequencies of each element in new_arr[] in the array freq[].
- Print the element ith maximum frequency as the required answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count the number of // clockwise array rotations required // to maximize count of array elements // present at indices same as their value void find_min_rot(int arr[], int n) { // Stores count of indices seperating // elements from its correct position int new_arr[n + 1]; int maxi = 1, ans = 0; // Stores frequencies of counts of // indices seperating int freq[n + 1]; for (int i = 1; i <= n; i++) { freq[i] = 0; } // Count indices separating each // element from its correct position for (int i = 1; i <= n; i++) { new_arr[i] = (arr[i] - i + n) % n; } // Update frequencies of counts obtaiend for (int i = 1; i <= n; i++) { freq[new_arr[i]]++; } // Find the count with maximum frequency for (int i = 1; i <= n; i++) { if (freq[i] > maxi) { maxi = freq[i]; ans = i; } } // Print the answer cout << ans << endl; } // Driver Code int main() { int N = 5; int arr[] = { -1, 3, 4, 1, 5, 2 }; // Find minimum number of // array rotations required find_min_rot(arr, N); return 0; } |
2
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Range sum queries for anticlockwise rotations of Array by K indices
- Count ways to arrange N distinct objects if all clockwise arrangements are considered the same
- Count of rotations required to generate a sorted array
- Maximize removal of adjacent array elements based on their absolute value
- Generating numbers that are divisor of their right-rotations
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Maximize product of same-indexed elements of same size subsequences
- Maximize length of longest non-decreasing array possible by the elements present in either ends of the given array
- Count the pairs in an array such that the difference between them and their indices is equal
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Number of strings which starts and ends with same character after rotations
- Rearrange given binary strings to maximize their Bitwise XOR value
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Program to find if two numbers and their AM and HM are present in an array using STL
- Find all matrix elements which are minimum in their row and maximum in their column
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
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.

