JavaScript Program to Find element at given index after a number of rotations


We will be implementing a JavaScript program to find an element at a given index after a number of rotations. This program will require us to perform rotations on an array and then return the element present at the specified index. To accomplish this task, we will be using the modulo operator to calculate the new index after each rotation. The future continuous tense will be used throughout the explanation.

In the program, we will be taking the input of the array, the number of rotations, and the index. We will then perform the rotations by using the modulo operator to find the new index after each rotation. The new index will be found by adding the number of rotations to the original index and then taking the modulo of the sum with the length of the array. The element at the final index will be returned as the result of the program.

In conclusion, we will be developing a simple and efficient program to find the element at a given index after a number of rotations. This program will demonstrate the use of the modulo operator and will be a valuable tool for anyone looking to perform array rotations in JavaScript.

Approach

Given an array of elements and a number of rotations k, the task is to find the element at a given index n after the rotations.

  • First, determine the actual position of the element after k rotations. The actual position can be found by taking the modulo of n and k.

  • Perform the rotations on the array by shifting the elements k times to the right.

  • To implement rotation, we can use the following approach −

    • store the last element in a temporary variable.

    • shift all elements one step to the right.

    • replace the first element with the temporary variable.

  • Repeat step 3 k times to complete all rotations.

  • After all the rotations, return the element at the nth index, which is now the actual position.

  • This approach has a time complexity of O(n * k), where n is the number of elements in the array. However, this can be optimized to O(n) by using the concept of cyclic rotations.

Example

Here is a JavaScript program that finds the element at a given index after a number of rotations −

function findElement(arr, rotations, index) {
   // Number of rotations modulo length of the array
   rotations = rotations % arr.length;
     
   // New index after rotations
   let newIndex = (arr.length + index - rotations) % arr.length;
    
   // Return element at new index
   return arr[newIndex];
}
let arr = [1, 2, 3, 4, 5];
let rotations = 2;
let index = 4;
console.log(findElement(arr, rotations, index)); // Output: 3

In this example, the result is 4, which is the element at index 3 after 2 rotations.

Explanation

  • The function findElement takes three parameters: arr, rotations, and index. arr is the array of elements, rotations is the number of rotations to be performed on the array, and index is the index of the element to be found after rotations.

  • The first line of the function calculates the number of rotations modulo the length of the array. This is done because if the number of rotations is greater than the length of the array, then after some rotations, the array will be back to its original state. So, we only need to perform rotations equivalent to the length of the array.

  • The next line calculates the new index of the element after the rotations have been performed. It does this by subtracting the number of rotations from the given index and adding the length of the array to the result. The final step is to take the modulo of the result with the length of the array.

  • Finally, the function returns the element at the new index.

  • In the code that follows, we initialize the array arr, the number of rotations, and the index index

  • The last line of the code calls the findElement function and prints the result to the console.

Updated on: 15-Mar-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements