November 15, 2022 |240 Views

    Javascript program to calculate Mth Element after K Left Rotations

      Share   Like
    Description
    Discussion

    In this video, we will write a Javascript program to calculate Mth Element after K Left Rotations. 
    So here we need to find the Mth element of the array after K left rotation in any given non-negative integers K, M, and an array arr[] with N elements. 

    Example:
    Input: arr[] = {32, 42, 53, 23}, K = 2, M = 1 
    Output: 53 

    Explanation: 
    The array after first left rotation a1[ ] = {42, 53, 23, 32} 
    The array after second left rotation a2[ ] = {53, 23, 32, 42} 
    1st element after 2 left rotations is 53. 

    Approach used:

    Simple Method :
    Step 1: Take array elements from a user and perform the left rotation K times. 
    Step 2: Finding the Mth elements of the final array after rotations. 

    Using formula (K+M-1)%N: In this method, we use the formula and find Mth elements. 

    Simple Method: The Time complexity O(1) and Auxiliary Space O(1)

    Calculate Mth Element after K Left Rotations 
    https://www.geeksforgeeks.org/find-the-mth-element-of-the-array-after-k-left-rotations/