C++ Program to Find the Mth element of the Array after K left rotations


In this problem, we need to print the element from the given index after rotating the array for K times.

This problem teaches us to find the element from the particular index in the rotational array. At first sight, the solution that comes to mind is that rotate the array for K times and access the Mth element, but we will also learn to access the Mth element in the rotational array without rotating the array.

Problem Statement

We have given arr[] array containing N numbers. Also, we have given the positive integer M and K. We need to rotate the arr[] array by K left rotations and return the Mth element of the array.

Sample Examples

Input –  arr[] = {10, 12, 34, 3, 7, 6}, K = 3, M = 2
Output – 7

Explanation

Let's rotate the array 3 times first.

  • The array is {12, 34, 3, 7, 6, 10} after the first rotation.

  • The array is {34, 3, 7, 6, 10, 12} after the second rotation.

  • The updated array after the final rotation is {3, 7, 6, 10, 12, 34}.

So, 2nd element in the updated array is 7.

Input –  arr[] = {76, 43, 4, 5, 9, 69, 12, 54}, K = 0, M = 5
Output – 9

Explanation

We don't need to rotate the array as K is 0. So, the 5th element in the original array is 9.

Input –  arr[] = {76, 43, 4, 5, 9, 69, 12, 54}, K = 10, M = 3
Output – 9

Explanation

The final array after 10 rotations is {4, 5, 9, 69, 12, 54, 76, 43}. So, 3rd element in the updated array is 9.

Approach 1

In this approach, we will swap all array elements for K times with its next element and the first element with the last element to rotate the array for K times.

After that, we will access the array element from the required index.

Algorithm

  • Step 1 − Use the loop to make total K iterations.

  • Step 2 − Initiaize the 'temp' variable with the arr[q] element.

  • Step 3 − After that, update the arr[q] with the arr[q + 1] element.

  • Step 4 − Next, update the arr[q + 1] with the temp element.

  • Step 5 − Return the array element from the M – 1 index.

Example

#include <bits/stdc++.h>
using namespace std;

int arr_rotation(int arr[], int arr_len, int K, int M) {
   // Rotate array by K left rotations
   for (int p = 0; p < K; p++) {
      for (int q = 0; q < arr_len - 1; q++) {
         int temp = arr[q];
         arr[q] = arr[q + 1];
         arr[q + 1] = temp;
      }
   }
   // Return element from M index
   return arr[M - 1];
}

int main() {
   int arr[] = {10, 12, 34, 3, 7, 6};
   int arr_len = sizeof(arr) / sizeof(arr[0]);
   int K = 3, M = 2;
   cout << "The array element after " << K << " rotations at the " << M << " index is " << arr_rotation(arr, arr_len, K, M);
   return 0;
}

Output

The array element after 3 rotations at the 2 index is 7
  • Time Complexity − O(N*K), where N is array length, and K is the total number of left rotations.

  • Space Complexity − O(1), as we use constant space.

Approach 2

If we rotate the array K times, the Kth (0-based index) becomes the array's first index. If K is greater than the array length, we need to make it smaller than the array length by taking its modulo with the array length.

After that, we can access the (M - 1)th element from the Kth index in the rotational array.

Algorithm

  • Step 1 − Perform the modulo operation of K with the array length.

  • Step 2 − Store the resultant value of the '(K + M - 1) % arr_len' into the 'ind' variable.

  • Step 3 − Access the array element from the 'ind' index and store it in the 'ele' variable.

  • Step 4 − Return the value of the 'ele' variable.

Example

#include <bits/stdc++.h>
using namespace std;

int arr_rotation(int arr[], int arr_len, int K, int M) {
   // Take modulo of K with arr_len
   K %= arr_len;
   // Get Mth element
   int ind = (K + M - 1) % arr_len;
   int ele = arr[ind];
   // return element
   return ele;
}

int main() {
   int arr[] = {10, 12, 34, 3, 7, 6};
   int arr_len = sizeof(arr) / sizeof(arr[0]);
   int K = 3, M = 2;
   cout << "The array element after " << K << " rotation at the " << M << " index is " << arr_rotation(arr, arr_len, K, M);
   return 0;
}

Output

The array element after 3 rotation at the 2 index is 7
  • Time Complexity − O(1)

  • Space Complexity − O(1)

We learned to access the Mth element after making K left rotations. Programmers may try to access the Mth element after making the K right rotations or (N – M)th element after making the K right rotations.

Updated on: 05-Oct-2023

33 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements