
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Element After K Right Rotations of an Array in C++
Right Rotations means we have to shift each element towards the right as the 0th index element shift to the 1st index, the 1st index element shift to the 2nd index, ..., and the last element shift to the 0th index. Here we have given an array of integers of size n, integer m, and integer k. Our task is to find the mth element after k right rotations of an array.
Here are some examples and explanations to help you understand the issue.
Sample Examples
Input
Array: [ 1, 3, 2, 5, 6, 7 ], k: 2, m: 4
Output
3
Explanation: 1st Right Rotation: [ 7, 1, 3, 2, 5, 6 ]
2nd Right Rotation: [ 6, 7, 1, 3, 2, 5 ]
The 4th (mth) element of the 2nd (kth) right rotation of an array is 3.
Input
Array: [ 6, 8, 9 ], k: 1, m: 1
Output
9
Explanation: 1st Right Rotation: [ 9, 6, 8 ]
The 1st (mth) element of the 1st (kth) right rotation of an array is 9.
Naive Approach
The idea of this approach is simple, first, we calculate the kth right rotation of a given array and then print the mth element of that right rotated array.
Let's see the code below for a better understanding of the above approach.
Example
C++ Program to Find Mth element after K Right Rotations of an Array.
#include <bits/stdc++.h> using namespace std; // left rotation of an array by ele void leftRotation(int n, int array [], int ele){ reverse (array, array + ele); reverse (array + ele, array + n); reverse (array, array + n); } // right rotation of an array by ele void rightRotation(int n, int array [], int ele){ leftRotation(n, array, n - ele); } // Create a function to find mth element and return it int findMthElement(int n, int array [], int k, int m){ // Call the rightRotation function to rotate given array k times for (int i=1; i<=k; i++) { rightRotation(n, array, 1); } // return the final mth element after k right rotation return array[m - 1]; } int main(){ int array [] = { 1, 3, 2, 5, 6, 7 }; //Given array int n = sizeof(array) / sizeof(array [0]); //Getting the size of the array int k = 2; //Given integer k int m = 4; //Given integer m int mthElement = findMthElement(n, array, k, m); cout << "mth element after the kth right rotation of an array is: "; cout<< mthElement; return 0; }
Output
mth element after the kth right rotation of an array is: 3
Time and Space Complexity
The time complexity of the above code is O(n * k), as we rotated an array of size n by k times.
The space complexity of the above code is O(1 ), as there is no extra space is used.
Mathematics Approach
In this approach, we have used maths. The concept of math is that the array is the same as after an n (size of an array) rotation. So, we can say that the kth rotation is the same as the k%n rotation.
So, we convert k = k%n, and now we are sure that the size of k is less than n the size of n array.
If the m is greater than equal to the k then the answer is (n-k) + (m-1) th element of the given array.
If the m is less than the k then the answer is (m-1-k) th element of the given array.
To further understand the above method, let's look at the code below.
Example
C++ Program to Find Mth element after K Right Rotations of an Array
#include <bits/stdc++.h> using namespace std; // Create a function to find mth element and return it int findMthElement(int n, int array [], int k, int m){ // as if k is greater than the size of array n k = k % n; // Create the mthElementIndex to store the index of mth element int MthElementIndex; if (k < m) { MthElementIndex = (m - k - 1); } else { MthElementIndex = (n - k) + (m - 1); } int mthElement = array [MthElementIndex]; // store the final answer return mthElement; } int main (){ int array [] = { 1, 3, 2, 5, 6, 7 }; //Given array int n = sizeof(array) / sizeof(array [0]); //Getting the size of the array int k = 2; //Given integer k int m = 4; //Given integer m int mthElement = findMthElement(n, array, k, m); cout << "mth element after the kth right rotation of an array is: "; cout<<mthElement; return 0; }
Output
mth element after the kth right rotation of an array is: 3
Time and Space Complexity
The time complexity of the above code is O(1).
The space complexity of the above code is O(1).
Conclusion
In this tutorial, we have implemented a C++ Program to Find the Mth element after the K Right Rotations of an Array. We have implemented two approaches naive approach and the mathematic approach. The time complexity is O(n*k) and O(1) respectively. Where n is the size of the array and k is the given k integer. The space complexity of both approaches is O(1).