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


In this problem, we will rotate the array by K left rotations and find the Mth element in the rotated array.

The naïve approach to solve the problem is to rotate the array by K left rotation and take the element from the M – 1 index.

The optimized approach is to find the final index value in such a way that the final index is the M – 1 index of the rotated array.

Problem Statement

We have given a nums[] array containing the positive integers. We have also given the positive integer K and M. We need to rotate the array by K left rotations and print the Mth element.

Sample Examples

Input –  nums[] = { 20, 30, 5, 6, 8, 23 }; K = 3, M = 3;
Output – 23

Explanation

  • After the first rotation, the array becomes {30, 5, 6, 8, 23, 20}.

  • After the second rotation, the updated array is {5, 6, 8, 23, 20, 30}.

  • After the final rotation, the array is {6, 8, 23, 20, 30, 5}.

The element in the 3rd position is 23 in the final array.

Input –  nums[] = { 20, 30, 5, 6, 8, 23 }; K = 0, M = 3;
Output – 5

Explanation

We need to make 0 rotations. So, the element at the 3rd position in the original array is 5.

Input –  nums[] = { 20, 30, 5, 6, 8, 23 }; K = 5, M = 2;
Output – 20

Explanation

The array after 5 left rotations will be {23, 20, 30, 5, 6, 8}, and the 2nd element in the updated array is 20.

Approach 1

In this approach, we will rotate the array by K-left rotations. After that, we will access the elements from the M – 1 index of the array.

Algorithm

  • Step 1 − Start traversing the array.

  • Step 2 − Store the first array element in the 'temp' variable.

  • Step 3 − Use another nested loop to traverse the array. In the nested loop, replace the nums[q] with the nums[q + 1].

  • Step 4 − Inside the outer loop, replace the nums[nums_len – 1] with the 'temp' value.

  • Step 5 − Return nums[M – 1] element.

Example

import java.util.*;

public class Main {
   public static int findElement(int[] nums, int nums_len, int K, int M){
      // Making K left rotations of array
      for (int p = 0; p < K; p++) {
         int temp = nums[0];
            for (int q = 0; q < nums_len - 1; ++q) {
            nums[q] = nums[q + 1];
         }
         nums[nums_len - 1] = temp;
      }
      // Return Mth element of rotated array
      return nums[M-1];
   }
   public static void main(String[] args) {
      int nums[] = { 20, 30, 5, 6, 8, 23 };
      int nums_len = nums.length;
      int K = 3, M = 3;
      System.out.println("The element at " + M + " index after rotating array by " + K + " left rotations is "
      + findElement(nums, nums_len, K, M));
   }
}

Output

The element at 3 index after rotating array by 3 left rotations is 23
  • Time Complexity − O(N*K) to make K left rotations.

  • Space Complexity − O(1), as we don't use any dynamic space.

Approach 2

In this approach, we will take the modulo of the K with the array's length. Because if we make left rotations equal to the array's length, we get the original array.

If we rotate the array by K rotation, we get the (K – 1)th element at the first position, and from there, we need to take the Mth element.

Algorithm

  • Step 1 − Take a modulo of K with the array's length.

  • Step 2 − Store the resultant value after taking a modulo of (K + M – 1) with the array's length into the 'ind' variable.

  • Step 3 − Access the array element from the 'ind' index, and return its value.

Example

import java.util.*;

public class Main {
   public static int findElement(int[] nums, int nums_len, int K, int M) {
      K %= nums_len;
      // Get the index of the Mth element
      int ind = (K + M - 1) % nums_len;
      return nums[ind];
   }
   public static void main(String[] args) {
      int nums[] = { 20, 30, 5, 6, 8, 23 };
      int nums_len = nums.length;
      int K = 3, M = 3;
      System.out.println("The element at " + M + " index after rotating array by " + K + " left rotations is "
      + findElement(nums, nums_len, K, M));
   }
}

Output

The element at 3 index after rotating array by 3 left rotations is 23
  • Time Complexity − O(1), as we don't traverse the array.

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

Conclusion

The first approach rotates the array for K times, but the second approach calculates the final index values based on the given K and M values to make the problem solution efficient.

Updated on: 05-Oct-2023

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements