Banana-Leopard/TASTY huggingface dataset:
# Python Program to search an element
# in a sorted and pivoted array
 
# Searches an element key in a pivoted
# sorted array arrp[] of size n
def pivotedBinarySearch(arr, n, key):
 
    pivot = findPivot(arr, 0, n-1)
 
    # If we didn't find a pivot,
    # then array is not rotated at all
    if pivot == -1:
        return binarySearch(arr, 0, n-1, key)
 
    # If we found a pivot, then first
    # compare with pivot and then
    # search in two subarrays around pivot
    if arr[pivot] == key:
        return pivot
    if arr[0] <= key:
        return binarySearch(arr, 0, pivot-1, key)
    return binarySearch(arr, pivot + 1, n-1, key)
 
 
# Function to get pivot. For array
# 3, 4, 5, 6, 1, 2 it returns 3
# (index of 6)
def findPivot(arr, low, high):
 
    # base cases
    if high < low:
        return -1
    if high == low:
        return low
 
    # low + (high - low)/2;
    mid = int((low + high)/2)
 
    if mid < high and arr[mid] > arr[mid + 1]:
        return mid
    if mid > low and arr[mid] < arr[mid - 1]:
        return (mid-1)
    if arr[low] >= arr[mid]:
        return findPivot(arr, low, mid-1)
    return findPivot(arr, mid + 1, high)
 
# Standard Binary Search function
def binarySearch(arr, low, high, key):
 
    if high < low:
        return -1
 
    # low + (high - low)/2;
    mid = int((low + high)/2)
 
    if key == arr[mid]:
        return mid
    if key > arr[mid]:
        return binarySearch(arr, (mid + 1), high,
                            key)
    return binarySearch(arr, low, (mid - 1), key)
 
 
# Driver program to check above functions
# Let us search 3 in below array
if __name__ == '__main__':
    arr1 = [5, 6, 7, 8, 9, 10, 1, 2, 3]
    n = len(arr1)
    key = 3
    print("Index of the element is : ", \
          pivotedBinarySearch(arr1, n, key))
 
# This is contributed by Smitha Dinesh Semwal

Time complexity: logn
Space complexity: constant
# Search an element in sorted and rotated array using
# single pass of Binary Search
 
# Returns index of key in arr[l..h] if key is present,
# otherwise returns -1
def search(arr, l, h, key):
    if l > h:
        return -1
 
    mid = (l + h) // 2
    if arr[mid] == key:
        return mid
 
    # If arr[l...mid] is sorted
    if arr[l] <= arr[mid]:
 
        # As this subarray is sorted, we can quickly
        # check if key lies in half or other half
        if key >= arr[l] and key <= arr[mid]:
            return search(arr, l, mid-1, key)
        return search(arr, mid + 1, h, key)
 
    # If arr[l..mid] is not sorted, then arr[mid... r]
    # must be sorted
    if key >= arr[mid] and key <= arr[h]:
        return search(arr, mid + 1, h, key)
    return search(arr, l, mid-1, key)
 
 
# Driver program
if __name__ == '__main__':
    arr = [4, 5, 6, 7, 8, 9, 1, 2, 3]
    key = 3
    i = search(arr, 0, len(arr)-1, key)
    if i != -1:
        print("Index: % d" % i)
    else:
        print("Key not found")
 
# This code is contributed by Shreyanshi Arun

Time complexity: logn
Space complexity: constant
# Python3 program to find a
# pair with a given sum in
# a sorted and rotated array
  
  
# This function returns True
# if arr[0..n-1] has a pair
# with sum equals to x.
def pairInSortedRotated(arr, n, x):
  
    # Find the pivot element
    for i in range(0, n - 1):
        if (arr[i] > arr[i + 1]):
            break
  
    # l is now index of smallest element
    l = (i + 1) % n
    # r is now index of largest element
    r = i
  
    # Keep moving either l
    # or r till they meet
    while (l != r):
  
        # If we find a pair with
        # sum x, we return True
        if (arr[l] + arr[r] == x):
            return True
  
        # If current pair sum is less,
        # move to the higher sum
        if (arr[l] + arr[r] < x):
            l = (l + 1) % n
        else:
  
            # Move to the lower sum side
            r = (n + r - 1) % n
  
    return False
  
  
# Driver program to test above function
arr = [11, 15, 6, 8, 9, 10]
X = 16
N = len(arr)
  
if (pairInSortedRotated(arr, N, X)):
    print("true")
else:
    print("false")
  
  
# This article contributed by saloni1297

Time complexity: linear
Space complexity: constant
'''Python program to find maximum value of Sum(i*arr[i])'''
 
# returns max possible value of Sum(i*arr[i])
 
 
def maxSum(arr):
 
    # stores sum of arr[i]
    arrSum = 0
 
    # stores sum of i*arr[i]
    currVal = 0
 
    n = len(arr)
 
    for i in range(0, n):
        arrSum = arrSum + arr[i]
        currVal = currVal + (i*arr[i])
 
    # initialize result
    maxVal = currVal
 
    # try all rotations one by one and find the maximum
    # rotation sum
    for j in range(1, n):
        currVal = currVal + arrSum-n*arr[n-j]
        if currVal > maxVal:
            maxVal = currVal
 
    # return result
    return maxVal
 
 
# test maxsum(arr) function
if __name__ == '__main__':
    arr = [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    print "Max sum is: ", maxSum(arr)

Time complexity: linear
Space complexity: constant
# A Naive Python3 program to find
# maximum sum rotation
import sys
 
# Returns maximum value of i * arr[i]
def maxSum(arr, n):
 
    # Initialize result
    res = -sys.maxsize
 
    # Consider rotation beginning with i
    # for all possible values of i.
    for i in range(0, n):
 
 
        # Initialize sum of current rotation
        curr_sum = 0
     
        # Compute sum of all values. We don't
        # actually rotation the array, but
        # compute sum by finding indexes when
        # arr[i] is first element
        for j in range(0, n):
         
            index = int((i + j)% n)
            curr_sum += j * arr[index]
     
 
        # Update result if required
        res = max(res, curr_sum)
    return res
 
# Driver code
arr = [8, 3, 1, 2]
n = len(arr)
 
print(maxSum(arr, n))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: quadratic
Space complexity: constant
# An efficient Python3 program to
# compute maximum sum of i * arr[i]
 
def maxSum(arr, n):
 
    # Compute sum of all array elements
    cum_sum = 0
     
    for i in range(0, n):
        cum_sum += arr[i]
 
    # Compute sum of i * arr[i] for
    # initial configuration.
    curr_val = 0
     
    for i in range(0, n):
        curr_val += i * arr[i]
 
    # Initialize result
    res = curr_val
 
    # Compute values for other iterations
    for i in range(1, n):
     
        # Compute next value using previous
        # value in O(1) time
        next_val = (curr_val - (cum_sum - arr[i-1]) +
                                    arr[i-1] * (n-1))
 
        # Update current value
        curr_val = next_val
 
        # Update result if required
        res = max(res, next_val)
     
    return res
 
 
# Driver code
arr = [8, 3, 1, 2]
n = len(arr)
 
print(maxSum(arr, n))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: linear
Space complexity: constant
# Python3 program to find maximum sum of
# all rotation of i*arr[i] using pivot.
 
# function definition
 
 
def maxSum(arr, n):
 
    sum = 0
    pivot = findPivot(arr, n)
 
    # difference in pivot and index
    # of last element of array
    diff = n - 1 - pivot
    for i in range(n):
        sum = sum + ((i + diff) % n) * arr[i]
 
    return sum
 
# function to find pivot
 
 
def findPivot(arr, n):
    for i in range(n):
 
        if(arr[i] > arr[(i + 1) % n]):
            return i
 
 
# Driver code
if __name__ == "__main__":
 
    # rotated input array
    arr = [8, 13, 1, 2]
    n = len(arr)
 
    max = maxSum(arr, n)
    print(max)
 
# This code is contributed by Ryuga

Time complexity: linear
Space complexity: constant
# Python3 program to find number
# of rotations in a sorted and
# rotated array.
 
# Returns count of rotations for
# an array which is first sorted
# in ascending order, then rotated
def countRotations(arr, n):
 
    # We basically find index
    # of minimum element
    min = arr[0]
    min_index = 0
    for i in range(0, n):
     
        if (min > arr[i]):
         
            min = arr[i]
            min_index = i
         
    return min_index;
 
 
# Driver code
arr = [15, 18, 2, 3, 6, 12]
n = len(arr)
print(countRotations(arr, n))
 
# This code is contributed by Smitha Dinesh Semwal

Time complexity: linear
Space complexity: constant
def countRotations(arr, n):
    # Check is array is rotated
    if (arr[0] > arr[n - 1]):
 
        # Traverse the array
        for i in range(0, n):
 
            # Index where element is greater
            # than the next element
            if (arr[i] > arr[i + 1]):
                return i + 1
 
    # Array is not rotated
    return 0
 
# Driver code
arr = [15, 18, 2, 3, 6, 12]
n = len(arr)
print(countRotations(arr, n))
 
# This code is contributed by karandeep1234

Time complexity: linear
Space complexity: constant
# Binary Search based Python3
# program to find number of
# rotations in a sorted and
# rotated array.
 
# Returns count of rotations for
# an array which is first sorted
# in ascending order, then rotated
 
def countRotations(arr, n):
    start = 0
    end = n-1
 
    # Finding the index of minimum of the array
    # index of min would be equal to number to rotation
    while start<=end:
        mid = start+(end-start)//2
         
        # Calculating the previous(prev)
        # and next(nex) index of mid
        prev = (mid-1+n)%n
        nex = (mid+1)%n
 
        # Checking if mid is minimum
        if arr[mid]<arr[prev]\
           and arr[mid]<=arr[nex]:
          return mid
       
        # if not selecting the unsorted part of array
        elif arr[mid]<arr[start]: end = mid-1
        elif arr[mid]>arr[end]: start = mid+1
        else: return 0
 
# Driver code
if __name__ == '__main__':
    arr = [15, 18, 2, 3, 6, 12]
    N = len(arr)
    print(countRotations(arr, N))
 
# This code is contributed by Smitha Dinesh Semwal

Time complexity: logn
Space complexity: logn
# Returns count of rotations for an array which
# is first sorted in ascending order, then rotated
 
# Observation: We have to return index of the smallest element
 
 
def countRotations(arr, n):
 
    low = 0
    high = n - 1
    while(low <= high):
 
        # if first element is mid or
        # last element is mid
        # then simply use modulo
        # so it never goes out of bound.
        mid = low + ((high - low) // 2)
        prev = (mid - 1 + n) % n
        next = (mid + 1) % n
 
        if(arr[mid] <= arr[prev]
           and arr[mid] <= arr[next]):
            return mid
        elif (arr[mid] <= arr[high]):
            high = mid - 1
        elif (arr[mid] >= arr[low]):
            low = mid + 1
    return 0
 
# Driver code
 
 
arr = [15, 18, 2, 3, 6, 12]
n = len(arr)
print(countRotations(arr, n))
 
# This code is contributed by shinjanpatra.

Time complexity: logn
Space complexity: constant
# Python3 implementation of left rotation
# of an array K number of times
 
# Fills temp with two copies of arr
 
 
def rotateArray(A, start, end):
 
    while start < end:
        temp = A[start]
        A[start] = A[end]
        A[end] = temp
        start += 1
        end -= 1
    return A
 
# Function to left rotate an array k times
 
 
def leftRotate(arr, a, k):
 
    # if the value of k ever exceeds the length of the array
    c = k % a
 
    # initializing array D so that we always
    # have a clone of the original array to rotate
    D = arr.copy()
 
    rotateArray(D, 0, c - 1)
    rotateArray(D, c, a - 1)
    rotateArray(D, 0, a - 1)
 
    # printing the rotated array
    print(D)
 
 
# Driver program
arr = [1, 3, 5, 7, 9]
n = len(arr)
 
k = 2
leftRotate(arr, n, k)
 
k = 3
leftRotate(arr, n, k)
 
k = 4
leftRotate(arr, n, k)
 
# This code is contributed by aditya942003patil

Time complexity: linear
Space complexity: linear
# Python3 implementation of left rotation
# of an array K number of times
 
# Fills temp with two copies of arr
 
 
def preprocess(arr, n):
    temp = [None] * (2 * n)
 
    # Store arr elements at i and i + n
    for i in range(n):
        temp[i] = temp[i + n] = arr[i]
    return temp
 
# Function to left rotate an array k times
 
 
def leftRotate(arr, n, k, temp):
 
    # Starting position of array after k
    # rotations in temp will be k % n
    start = k % n
 
    # Print array after k rotations
    for i in range(start, start + n):
        print(temp[i], end=" ")
    print("")
 
 
# Driver program
arr = [1, 3, 5, 7, 9]
n = len(arr)
temp = preprocess(arr, n)
 
k = 2
leftRotate(arr, n, k, temp)
 
k = 3
leftRotate(arr, n, k, temp)
 
k = 4
leftRotate(arr, n, k, temp)
 
# This code is contributed by Sanghamitra Mishra

Time complexity: linear
Space complexity: linear
# Python3 implementation of
# left rotation of an array
# K number of times
 
# Function to left rotate
# an array k times
 
 
def leftRotate(arr, n, k):
 
    # Print array
    # after k rotations
    for i in range(k, k + n):
        print(str(arr[i % n]),
              end=" ")
 
 
# Driver Code
arr = [1, 3, 5, 7, 9]
n = len(arr)
k = 2
leftRotate(arr, n, k)
print()
 
k = 3
leftRotate(arr, n, k)
print()
 
k = 4
leftRotate(arr, n, k)
print()
 
# This code is contributed
# by ChitraNayal

Time complexity: linear
Space complexity: constant
# python3 code  to implement the approach
 
def findMin(arr, N):
     
    min_ele = arr[0];
 
    # Traversing over array to
    # find minimum element
    for i in range(N) :
        if arr[i] < min_ele :
            min_ele = arr[i]
 
    return min_ele;
 
# Driver program
arr = [5, 6, 1, 2, 3, 4]
N = len(arr)
 
print(findMin(arr,N))
 
# This code is contributed by aditya942003patil

Time complexity: linear
Space complexity: constant
# Python program to find minimum element
# in a sorted and rotated array
 
 
def findMin(arr, low, high):
    # This condition is needed to handle the case when array is not
    # rotated at all
    if high < low:
        return arr[0]
 
    # If there is only one element left
    if high == low:
        return arr[low]
 
    # Find mid
    mid = int((low + high)/2)
 
    # Check if element (mid+1) is minimum element. Consider
    # the cases like [3, 4, 5, 1, 2]
    if mid < high and arr[mid+1] < arr[mid]:
        return arr[mid+1]
 
    # Check if mid itself is minimum element
    if mid > low and arr[mid] < arr[mid - 1]:
        return arr[mid]
 
    # Decide whether we need to go to left half or right half
    if arr[high] > arr[mid]:
        return findMin(arr, low, mid-1)
    return findMin(arr, mid+1, high)
 
 
# Driver program to test above functions
if __name__ == '__main__':
    arr = [5, 6, 1, 2, 3, 4]
    N = len(arr)
    print("The minimum element is " + \
          str(findMin(arr, 0, N-1)))
 
# This code is contributed by Pratik Chhajer

Time complexity: logn
Space complexity: constant
# Python3 program for right rotation of
# an array (Reversal Algorithm)
 
 
# Function to reverse arr
# from index start to end
def reverseArray( arr, start, end):
     
    while (start < end):
         
        arr[start], arr[end] = arr[end], arr[start]
        start = start + 1
        end = end - 1
     
 
# Function to right rotate arr
# of size n by d
def rightRotate( arr, d, n):
     
    reverseArray(arr, 0, n - 1);
    reverseArray(arr, 0, d - 1);
    reverseArray(arr, d, n - 1);
 
 
# function to print an array
def printArray( arr, size):
    for i in range(0, size):
        print (arr[i], end = ' ')
 
 
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = len(arr)
k = 3
     
# Function call
rightRotate(arr, k, n)
printArray(arr, n)
 
 
# This article is contributed
# by saloni1297

Time complexity: linear
Space complexity: constant
# Python3 code to Find another array
# such that the hamming distance
# from the original array is maximum
 
# Return the maximum hamming
# distance of a rotation
def maxHamming( arr , n ):
 
    # arr[] to brr[] two times so
    # that we can traverse through
    # all rotations.
    brr = [0] * (2 * n + 1)
    for i in range(n):
        brr[i] = arr[i]
    for i in range(n):
        brr[n+i] = arr[i]
     
    # We know hamming distance
    # with 0 rotation would be 0.
    maxHam = 0
     
    # We try other rotations one by
    # one and compute Hamming
    # distance of every rotation
    for i in range(1, n):
        currHam = 0
        k = 0
        for j in range(i, i + n):
            if brr[j] != arr[k]:
                currHam += 1
                k = k + 1
         
        # We can never get more than n.
        if currHam == n:
            return n
         
        maxHam = max(maxHam, currHam)
     
    return maxHam
 
# driver program
arr = [2, 4, 6, 8]
n = len(arr)
print(maxHamming(arr, n))
 
# This code is contributed by "Sharad_Bhardwaj".

Time complexity: quadratic
Space complexity: linear
# Python3 program to Find another array
# such that the hamming distance
# from the original array is maximum
 
# requires O(n*n) time and O(1) extra space;
 
# Return the maximum hamming distance of a rotation
def maxHamming(arr, n):
     
    # outer loop for how much rotation
    hmmd = 0
 
    for j in range(1,n):
        hmmd = 0
        #inner loop to compare elements with elements on shifted index
        for i in range(n):
            if(arr[i] != arr[(i + j) % n]):
                hmmd += 1
         
            #max possible hamming distance is n, no need to check further
        if(hmmd == n):
            return n
    return hmmd
 
# driver program
arr = [2, 4, 6, 8]
n = len(arr)
print(maxHamming(arr, n))   
 
 
# This code is contributed by shinjanpatra

Time complexity: quadratic
Space complexity: constant
# Python code to find maximum
# of an array with it's rotations
import time
 
# Function hamming distance to find
# the hamming distance for two lists/strings
def hamming_distance(x: list, y: list):
   
    # Initialize count
    count = 0
     
    # Run loop for size of x(or y)
    # as both as same length
    for i in range(len(x)):
       
        # Check if corresponding elements
        # at same index are not equal
        if(x[i] != y[i]):
           
            # Increment the count every
            # time above condition satisfies
            count += 1
             
    # Return the hamming distance
    # for given pair of lists or strings
    return count
 
 
# Function to rotate the given array
# in anti-clockwise direction by 1
def rotate_by_one(arr: list):
   
    # Store 1st element in a variable
    x = arr[0]
     
    # Update each ith element (0<=i<=n-2)
    # with it's next value
    for i in range(0, len(arr)-1):
        arr[i] = arr[i+1]
       
    # Assign 1st element to the last index
    arr[len(arr)-1] = x
 
 
# Function max_hamming_distance to find
# the maximum hamming distance for given array
def max_hamming_distance(arr: list):
   
    # Initialize a variable to store
    # maximum hamming distance
    max_h = -10000000000
     
    # Store size of the given array
    # in a variable n
    n = len(arr)
     
    # Initialize a new array
    a = []
     
    # Copy the original array in new array
    for i in range(n):
        a.append(arr[i])
         
    # Run loop for i=0 to i=n-1 for n-1 rotations
    for i in range(1, n):
       
        # Find the next rotation
        rotate_by_one(arr)
        print("Array after %d rotation : " % (i), arr)
         
        # Store hamming distance of current
        # rotation with original array
        curr_h_dist = hamming_distance(a, arr)
        print("Hamming Distance with %d rotations: %d" %
                                        (i, curr_h_dist))
         
        # Check if current hamming distance
        # is greater than max hamming distance
        if curr_h_dist > max_h:
           
            # If yes, assign value of current
            # hamming distance to max hamming distance
            max_h = curr_h_dist
             
        print('\n')
    # Return maximum hamming distance
    return max_h
 
 
# Driver code
if __name__ == '__main__':
   
    arr = [3, 0, 6, 4, 3]
    start = time.time()
    print('\n')
    print("Original Array : ", arr)
    print('\n')
    print("Maximum Hamming Distance: ", max_hamming_distance(arr))
    end = time.time()
    print(f"Execution Time = {end - start}")
# This code is contributed by Vivek_Kumar_Sinha

Time complexity: quadratic
Space complexity: constant
# Python Program to solve queries on Left and Right
# Circular shift on array
 
# Function to solve query of type 1 x.
def querytype1(toRotate, times, n):
   
    # Decreasing the absolute rotation
    toRotate = (toRotate - times) % n
    return toRotate
 
# Function to solve query of type 2 y.
def querytype2(toRotate, times, n):
   
    # Increasing the absolute rotation.
    toRotate = (toRotate + times) % n
    return toRotate
 
# Function to solve queries of type 3 l r.
def querytype3( toRotate, l, r, preSum, n):
   
    # Finding absolute l and r.
    l = (l + toRotate + n) % n
    r = (r + toRotate + n) % n
 
    # if l is before r.
    if (l <= r):
        print((preSum[r + 1] - preSum[l]))  
 
    # If r is before l.
    else:
        print((preSum[n] + preSum[r + 1] - preSum[l]))
 
# Wrapper Function solve all queries.
def wrapper( a, n):
    preSum = [ 0 for i in range(n + 1)]
     
    # Finding Prefix sum
    for i in range(1,n+1):
        preSum[i] = preSum[i - 1] + a[i - 1]
 
    toRotate = 0
 
    # Solving each query
    toRotate = querytype1(toRotate, 3, n)
    querytype3(toRotate, 0, 2, preSum, n)
    toRotate = querytype2(toRotate, 1, n)
    querytype3(toRotate, 1, 4, preSum, n);
 
# Driver Program
if __name__ == '__main__':
    arr = [ 1, 2, 3, 4, 5 ]
    N = len(arr)
    wrapper(arr, N)
 
# This code is contributed by rohan07.

Time complexity: linear
Space complexity: linear
# Python implementation of left rotation of
# an array K number of times
 
# Function to leftRotate array multiple times
 
 
def leftRotate(arr, n, k):
 
    # To get the starting point of rotated array
    mod = k % n
    s = ""
 
    # Prints the rotated array from start position
    for i in range(n):
        print str(arr[(mod + i) % n]),
    print
    return
 
 
# Driver code
arr = [1, 3, 5, 7, 9]
n = len(arr)
k = 2
 
# Function Call
leftRotate(arr, n, k)
 
k = 3
 
# Function Call
leftRotate(arr, n, k)
 
k = 4
 
# Function Call
leftRotate(arr, n, k)
 
# This code is contributed by Sachin Bisht

Time complexity: linear
Space complexity: constant
# Python code for above approach
 
import array as arr
def findElement(arr, ranges, rotations, index):
 
    # Track of the rotation number
    n1 = 1
 
    # Track of the row index for the ranges[][] array
    i = 0
 
    # Initialize the left side of the ranges[][] array
    leftRange = 0
 
    # Initialize the right side of the ranges[][] array
    rightRange = 0
 
    # Initialize the key variable
    key = 0
    while (n1 <= rotations):
 
        leftRange = ranges[i][0]
        rightRange = ranges[i][1]
        key = arr[rightRange]
        j = rightRange
        while(j <= rightRange & j > leftRange):
            arr[j] = arr[j - 1]
            j = j-1
        arr[leftRange] = key
 
        n1 = n1+1
        i = i+1
    # Return the element after all the rotations
    return arr[index]
 
 
# driver code
ar = arr.array('i', [1, 2, 3, 4, 5])
 
# No. of rotations
rotations = 2
 
# Ranges according to 0-based indexing
ranges = [[0, 2], [0, 3]]
 
index = 1
print(findElement(ar, ranges, rotations, index))
 
# This code is contributed by adityamaharshi21.

Time complexity: quadratic
Space complexity: constant
# Python 3 code to rotate an array
# and answer the index query
 
# Function to compute the element
# at given index
def findElement(arr, ranges, rotations, index) :
     
    for i in range(rotations - 1, -1, -1 ) :
     
        # Range[left...right]
        left = ranges[i][0]
        right = ranges[i][1]
 
        # Rotation will not have
        # any effect
        if (left <= index and right >= index) :
            if (index == left) :
                index = right
            else :
                index = index - 1
         
    # Returning new element
    return arr[index]
 
# Driver Code
arr = [ 1, 2, 3, 4, 5 ]
 
# No. of rotations
rotations = 2
 
# Ranges according to
# 0-based indexing
ranges = [ [ 0, 2 ], [ 0, 3 ] ]
 
index = 1
 
print(findElement(arr, ranges, rotations, index))
     
# This code is contributed by Nikita Tiwari.

Time complexity: constant
Space complexity: constant