- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Mth Eelement after K Right Rotations of an Array
Right rotating an array means shifting its elements to the right by a certain number of positions. In a single right rotation, the last element of the array becomes the first element, and the remaining elements are shifted to the right.
Problem Statement
The goal is to find the Mth element of an array after performing K right rotations, where K and M are non-negative integers and the array contains N elements.
Sample Examples
Input
arr = [12 34 56 21], K = 2, M = 1
Output
56
Explanation
Arr after K right rotations = [56 21 12 34]
Element at 1st position = 56
Input
arr = [0 3 1 5 7 2 2], K = 6, M = 4
Output
7
Explanation
Arr after K right rotations = [3 1 5 7 2 2 0]
Element at 4th position = 7
Solution Approach 1
The solution approach we will discuss views the problem statement as two fold. There are two objectives to accomplish here. They are as follows:
Right rotate the array K times.
Return the Mth element of the modified array.
Task 1: It can be achieved by making use of the in-built reverse() function. The following dry run demonstrates this perfectly.
Let the original vector arr be {1, 2, 3, 4, 5}
Let the number of right rotations(K) be 2
Original arr
1 | 2 | 3 | 4 | 5 |
Reverse the last K elements of arr
1 | 2 | 3 | 5 | 4 |
Reverse the initial N - K elements of arr
3 | 2 | 1 | 5 | 4 |
Now reverse the entire arr
4 | 5 | 1 | 2 | 3 |
Thus after performing 3 reverse operations we get arr rotated right K times.
Task 2: Now to find the Mth element, we simply return the element of arr at the (M - 1)th index since we follow 0 based indexing.
Pseudocode
function rightRotateByk(arr, k)
reverse(last K elements)
reverse(N - K initial elements)
reverse(all the elements)
end function
function solve(arr, k, m)
rightRotateByk(arr, k)
return arr[m - 1]
end function
function main()
Define arr[ ]
Initialise k
Initialise m
Declare ans
Function call solve(arr, k, m)
Store the result in ans
Output ans
end function
Example: C++ Program
The code determines the element at the Mth position after subjecting an original array to K right rotations. The rightRotateByk() function is defined to rotate the array arr K times in a clockwise manner. Inside the rightRotateByk function, three reverse operations are performed:
The last K elements of arr are reversed using the range (arr.begin() + arr.size() - k, arr.end()).
The initial elements of arr (excluding the last K elements) are reversed using the range (arr.begin(), arr.begin() + arr.size() - k).
All the elements of arr are reversed to complete the right rotations.
The Mth element of the rotated array is returned by accessing arr[m - 1], as array indices start from 0.
Example
// C++ program to determine the element at the Mth number after subjecting the original array to k right rotations #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> using namespace std; // Function to rotate arr K times in clockwise manner void rightRotateByk(vector<int> &arr, int k){ // reverse the last K elements reverse(arr.begin() + arr.size() - k, arr.end()); // reverse the initial elements other than the last k elements reverse(arr.begin(), arr.begin() + arr.size() - k); // reverse all the elements to right rotate the original arr K times reverse(arr.begin(), arr.end()); } // Function to return the Mth element of arr after subjecting arr to K right rotations int solve(vector<int> &arr, int k, int m){ rightRotateByk(arr, k); // Return the Mth element of arr return arr[m - 1]; } int main(){ vector<int> arr = {5, 7, 2, 8, 0}; int k, m; k = 2; m = 2; int ans = solve(arr, k, m); cout << "Mth element after K right rotations is: " << ans << endl; return 0; }
Output
Mth element after K right rotations is: 0
Time and Space Complexity Analysis
Time Complexity: O(n)
The rightRotateByk function performs three reverse operations, each of which takes linear time. Therefore, the time complexity of rightRotateByk is O(n), where n is the size of the array.
Additionally, accessing the Mth element of the array takes constant time.
Space Complexity: O(1)
The code does not make use of any auxiliary space other than the vector to store the input array.
Solution Approach 2
A simple insight can optimize the code to a great extent. The key observation is that after N rotations, the original array is restored because the elements wrap around to their original positions. Based on this observation, we can utilize the modulo operator % to determine the effective number of rotations.
For any positive integer K, K%N will yield a value in the range of 0 to N-1.
If K is greater than or equal to M (K >= M), it means that the Mth element in the original array is within the K right-rotated portion. In this case, we calculate the index of the Mth element in the original array as (N - K) + (M - 1).
(N - K) represents the number of elements that were shifted to the right by K rotations.
(M - 1) represents the index offset within the shifted portion.
If K is less than M (K < M), it means that the Mth element in the original array is within the portion that remains at the beginning after the right rotations. In this case, we calculate the index of the Mth element in the original array as (M - K - 1).
(M - K - 1) represents the index of the element in the remaining portion at the beginning of the array.
Example: C++ Program
The code determines the Mth element of an array after performing K right rotations. It handles cases where K exceeds the array size by using the modulo operator. The index of the Mth element is calculated based on the relationship between K and M. Finally, it returns the element at that index in the original array after the rotations.
Example
// C++ program to determine the element at the Mth number after subjecting the original array to k right rotations #include <iostream> #include <vector> #include <algorithm> using namespace std; // Function to return the Mth element if the original array is subjected to K right rotations int solve(vector<int> arr, int K, int M){ int N = arr.size(); // (K % N) will yield a value in the range of 0 to N-1 K %= N; int ind; if (K >= M) { // This implies that the Mth element in the original array is within the K right-rotated portion ind = (N - K) + (M - 1); } else{ // This means that the Mth element in the original array is within the portion that remains at the beginning after the right rotations. ind = (M - K - 1); } return arr[ind]; } int main(){ vector<int> arr = {0, 3, 1, 5, 7, 2, 2}; int k, m; k = 6; m = 4; int ans = solve(arr, k, m); cout << "Mth element after K right rotations is: " << ans << endl; return 0; }
Output
Mth element after K right rotations is: 7
Time and Space Complexity Analysis
The program runs in constant time and requires no auxiliary space. Thus the time and space complexity of the program is O(1).
Conclusion
The article discusses two approaches to return the Mth element of the array if it were to be rotated K times to the right. It provides the C++ program code, pseudocode, as well as the time and space complexity analysis.