Banana-Leopard/TASTY huggingface dataset:
from heapq import heappush, heappop
from bisect import bisect, insort
 
 
def getNumOfInversions(A):
    N = len(A)
    if N <= 1:
        return 0
 
    sortList = []
    result = 0
 
    # Heapsort, O(N*log(N))
    for i, v in enumerate(A):
        heappush(sortList, (v, i))
 
    # Create a sorted list of indexes
    x = []
    while sortList:
       
        # O(log(N))
        v, i = heappop(sortList)
         
        # Find the current minimum's index
        # the index y can represent how many minimums on the left
        y = bisect(x, i)
         
        # i can represent how many elements on the left
        # i - y can find how many bigger nums on the left
        result += i - y
 
        insort(x, i)
 
    return result
 
# Driver Code
if __name__ == '__main__':
    A = [1, 20, 6, 4, 5]
    result = getNumOfInversions(A)
    print(f'Number of inversions are {result}')

Time complexity: nlogn
Space complexity: linear
# Python3 program to find shortest
# subarray which is unsorted
 
# Bool function for checking an array 
# elements are in increasing
def increasing(a, n):
 
    for i in range(0, n - 1):
        if (a[i] >= a[i + 1]):
            return False
             
    return True
 
# Bool function for checking an array
# elements are in decreasing
def decreasing(a, n):
 
    for i in range(0, n - 1):
        if (a[i] < a[i + 1]):
            return False
             
    return True
 
def shortestUnsorted(a, n):
 
    # increasing and decreasing are two functions.
    # if function return True value then print
    # 0 otherwise 3.
    if (increasing(a, n) == True or
        decreasing(a, n) == True):
        return 0
    else:
        return 3
 
# Driver code
ar = [7, 9, 10, 8, 11]
n = len(ar)
print(shortestUnsorted(ar, n))
 
# This code is contributed by Smitha Dinesh Semwal.

Time complexity: linear
Space complexity: constant
# Python3 program to find 
# minimum number of swaps
# required to sort an array
 
# Function returns the minimum
# number of swaps required to
# sort the array
def minSwaps(arr):
    n = len(arr)
     
    # Create two arrays and use
    # as pairs where first array
    # is element and second array
    # is position of first element
    arrpos = [*enumerate(arr)]
     
    # Sort the array by array element
    # values to get right position of
    # every element as the elements
    # of second array.
    arrpos.sort(key = lambda it : it[1])
     
    # To keep track of visited elements.
    # Initialize all elements as not
    # visited or false.
    vis = {k : False for k in range(n)}
     
    # Initialize result
    ans = 0
    for i in range(n):
         
        # already swapped or
        # already present at
        # correct position
        if vis[i] or arrpos[i][0] == i:
            continue
             
        # find number of nodes
        # in this cycle and
        # add it to ans
        cycle_size = 0
        j = i
         
        while not vis[j]:
             
            # mark node as visited
            vis[j] = True
             
            # move to next node
            j = arrpos[j][0]
            cycle_size += 1
             
        # update answer by adding
        # current cycle
        if cycle_size > 0:
            ans += (cycle_size - 1)
             
    # return answer
    return ans
 
# Driver Code    
arr = [1, 5, 4, 3, 2]
print(minSwaps(arr))
 
# This code is contributed
# by Dharan Aditya

Time complexity: nlogn
Space complexity: linear
# Function returns the
# minimum number of swaps
# required to sort the array
from functools import cmp_to_key
 
def cmp(a, b):
    return a - b
 
def minSwaps(nums):
    Len = len(nums)
    map = {}
    for i in range(Len):
        map[nums[i]] = i
 
    nums = sorted(nums, key = cmp_to_key(cmp))
 
    # To keep track of visited elements. Initialize
    # all elements as not visited or false.
    visited = [False for col in range(Len)]
     
    # Initialize result
    ans = 0
    for i in range(Len):
 
        # already swapped and corrected or
        # already present at correct pos
        if (visited[i] or map[nums[i]] == i):
            continue
 
        j,cycle_size = i, 0
        while (visited[j] == False):
            visited[j] = True
 
            # move to next node
            j = map[nums[j]]
            cycle_size += 1
 
        # Update answer by adding current cycle.
        if (cycle_size > 0):
            ans += (cycle_size - 1)
 
    return ans
 
# Driver program to test the above function
a = [ 1, 5, 4, 3, 2 ]
print(minSwaps(a))
     
# This code is contributed by shinjanpatra

Time complexity: nlogn
Space complexity: linear
# Python3 program to find
#minimum number of swaps
# required to sort an array
 
# Return the minimum number
# of swaps required to sort
# the array
def minSwaps(arr, N):
     
    ans = 0
    temp = arr.copy()
    temp.sort()
    for i in range(N):
       
        # This is checking whether
        # the current element is
        # at the right place or not
        if (arr[i] != temp[i]):
            ans += 1
 
            # Swap the current element
            # with the right index
            # so that arr[0] to arr[i]
            # is sorted
            swap(arr, i,
                 indexOf(arr, temp[i]))
   
    return ans
   
def swap(arr, i, j):
     
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
     
def indexOf(arr, ele):
    
    for i in range(len(arr)):       
        if (arr[i] == ele):
                return i
    return -1
 
# Driver code
if __name__ == "__main__":
      a = [101, 758, 315, 730,
           472, 619, 460, 479]
      n = len(a)
       
      # Output will be 5
      print(minSwaps(a, n))
 
# This code is contributed by Chitranayal

Time complexity: quadratic
Space complexity: linear
# Python3 program to find
# minimum number of swaps
# required to sort an array
 
# Return the minimum number
# of swaps required to sort
# the array
def minSwap(arr, n):
     
    ans = 0
    temp = arr.copy()
 
    # Dictionary which stores the
      # indexes of the input array
    h = {}
 
    temp.sort()
 
    for i in range(n):
         
        #h.[arr[i]
        h[arr[i]] = i
         
    init = 0
     
    for i in range(n):
 
        # This is checking whether
        # the current element is
        # at the right place or not
        if (arr[i] != temp[i]):
            ans += 1
            init = arr[i]
 
            # If not, swap this element
              # with the index of the
              # element which should come here
            arr[i], arr[h[temp[i]]] = arr[h[temp[i]]], arr[i]
 
            # Update the indexes in
              # the hashmap accordingly
            h[init] = h[temp[i]]
            h[temp[i]] = i
             
    return ans
 
# Driver code
a = [ 101, 758, 315, 730,
      472, 619, 460, 479 ]
n = len(a)
 
# Output will be 5
print(minSwap(a, n))
 
# This code is contributed by avanitrachhadiya2155

Time complexity: nlogn
Space complexity: linear
# Python program to sort an array with
# 0, 1 and 2 in a single pass
 
# Function to sort array
 
 
def sort012(a, arr_size):
    lo = 0
    hi = arr_size - 1
    mid = 0
    # Iterate till all the elements
    # are sorted
    while mid <= hi:
        # If the element is 0
        if a[mid] == 0:
            a[lo], a[mid] = a[mid], a[lo]
            lo = lo + 1
            mid = mid + 1
        # If the element is 1
        elif a[mid] == 1:
            mid = mid + 1
        # If the element is 2
        else:
            a[mid], a[hi] = a[hi], a[mid]
            hi = hi - 1
    return a
 
# Function to print array
 
 
def printArray(a):
    for k in a:
        print(k, end=' ')
 
 
# Driver Program
arr = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
arr_size = len(arr)
arr = sort012(arr, arr_size)
printArray(arr)
 
# Contributed by Harshit Agrawal

Time complexity: linear
Space complexity: constant
# Python implementation of the approach
 
# Utility function to print contents of an array
 
 
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end=" ")
 
 
# Function to sort the array of 0s, 1s and 2s
def sortArr(arr, n):
    cnt0 = 0
    cnt1 = 0
    cnt2 = 0
 
    # Count the number of 0s, 1s and 2s in the array
    for i in range(n):
        if arr[i] == 0:
            cnt0 += 1
 
        elif arr[i] == 1:
            cnt1 += 1
 
        elif arr[i] == 2:
            cnt2 += 1
 
    # Update the array
    i = 0
 
    # Store all the 0s in the beginning
    while (cnt0 > 0):
        arr[i] = 0
        i += 1
        cnt0 -= 1
 
    # Then all the 1s
    while (cnt1 > 0):
        arr[i] = 1
        i += 1
        cnt1 -= 1
 
    # Finally all the 2s
    while (cnt2 > 0):
        arr[i] = 2
        i += 1
        cnt2 -= 1
 
    # Print the sorted array
    printArr(arr, n)
 
 
# Driver code
 
arr = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
n = len(arr)
 
sortArr(arr, n)
 
# This code is contributed by shubhamsingh10

Time complexity: linear
Space complexity: constant
# Function to find position to insert current element of
# stream using binary search
def binarySearch(arr, item, low, high):
 
    if (low >= high):
        return (low + 1) if (item > arr[low]) else low
 
    mid = (low + high) // 2
 
    if (item == arr[mid]):
        return mid + 1
 
    if (item > arr[mid]):
        return binarySearch(arr, item, mid + 1, high)
 
    return binarySearch(arr, item, low, mid - 1)
 
# Function to print median of stream of integers
def printMedian(arr, n):
 
    i, j, pos, num = 0, 0, 0, 0
    count = 1
 
    print(f"Median after reading 1 element is {arr[0]}")
 
    for i in range(1, n):
        median = 0
        j = i - 1
        num = arr[i]
 
        # find position to insert current element in sorted
        # part of array
        pos = binarySearch(arr, num, 0, j)
 
        # move elements to right to create space to insert
        # the current element
        while (j >= pos):
            arr[j + 1] = arr[j]
            j -= 1
 
        arr[j + 1] = num
 
        # increment count of sorted elements in array
        count += 1
 
        # if odd number of integers are read from stream
        # then middle element in sorted order is median
        # else average of middle elements is median
        if (count % 2 != 0):
            median = arr[count // 2]
 
        else:
            median = (arr[(count // 2) - 1] + arr[count // 2]) // 2
 
        print(f"Median after reading {i + 1} elements is {median} ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4]
    n = len(arr)
 
    printMedian(arr, n)
 
# This code is contributed by rakeshsahni

Time complexity: quadratic
Space complexity: constant
# Python code to implement the approach
 
from heapq import heappush, heappop, heapify
import math
 
# Function to find the median of stream of data
def streamMed(arr, N):
     
    # Declaring two min heap
    g = []
    s = []
    for i in range(len(arr)):
       
        # Negation for treating it as max heap
        heappush(s, -arr[i])
        heappush(g, -heappop(s))
        if len(g) > len(s):
            heappush(s, -heappop(g))
 
        if len(g) != len(s):
            print(-s[0])
        else:
            print((g[0] - s[0])/2)
 
 
# Driver code
if __name__ == '__main__':
    A = [5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4]
    N = len(A)
     
    # Function call
    streamMed(A, N)

Time complexity: nlogn
Space complexity: linear
# Python3 code to count the number of
# possible triangles using brute
# force approach
 
# Function to count all possible
# triangles with arr[] elements
 
 
def findNumberOfTriangles(arr, n):
 
    # Count of triangles
    count = 0
 
    # The three loops select three
    # different values from array
    for i in range(n):
        for j in range(i + 1, n):
 
            # The innermost loop checks for
            # the triangle property
            for k in range(j + 1, n):
 
                # Sum of two sides is greater
                # than the third
                if (arr[i] + arr[j] > arr[k] and
                    arr[i] + arr[k] > arr[j] and
                        arr[k] + arr[j] > arr[i]):
                    count += 1
    return count
 
 
# Driver code
if __name__ == "__main__":
    arr = [10, 21, 22, 100, 101, 200, 300]
    size = len(arr)
 
    # Function call
    print("Total number of triangles possible is",
          findNumberOfTriangles(arr, size))
 
# This code is contributed by shubhamsingh10

Time complexity: cubic
Space complexity: constant
# Python3 function to count all possible triangles with arr[]
# elements
 
 
def findnumberofTriangles(arr):
 
    # Sort array and initialize count as 0
    n = len(arr)
    arr.sort()
    count = 0
 
    # Fix the first element. We need to run till n-3 as
    # the other two elements are selected from arr[i + 1...n-1]
    for i in range(0, n-2):
 
        # Initialize index of the rightmost third element
        k = i + 2
 
        # Fix the second element
        for j in range(i + 1, n):
 
            # Find the rightmost element which is smaller
            # than the sum of two fixed elements
            # The important thing to note here is, we use
            # the previous value of k. If value of arr[i] +
            # arr[j-1] was greater than arr[k], then arr[i] +
            # arr[j] must be greater than k, because the array
            # is sorted.
            while (k < n and arr[i] + arr[j] > arr[k]):
                k += 1
 
            # Total number of possible triangles that can be
            # formed with the two fixed elements is k - j - 1.
            # The two fixed elements are arr[i] and arr[j]. All
            # elements between arr[j + 1] to arr[k-1] can form a
            # triangle with arr[i] and arr[j]. One is subtracted
            # from k because k is incremented one extra in above
            # while loop. k will always be greater than j. If j
            # becomes equal to k, then above loop will increment k,
            # because arr[k] + arr[i] is always greater than arr[k]
            if(k > j):
                count += k - j - 1
 
    return count
 
 
# Driver code
if __name__ == "__main__":
    arr = [10, 21, 22, 100, 101, 200, 300]
 
    # Function call
    print("Total number of Triangles:", findnumberofTriangles(arr))
 
# This code is contributed by Devesh Agrawal

Time complexity: quadratic
Space complexity: constant
# Python implementation of the above approach
def CountTriangles(A):
 
    n = len(A)
 
    A.sort()
 
    count = 0
 
    for i in range(n - 1, 0, -1):
        l = 0
        r = i - 1
        while(l < r):
            if(A[l] + A[r] > A[i]):
 
                # If it is possible with a[l], a[r]
                # and a[i] then it is also possible
                # with a[l + 1]..a[r-1], a[r] and a[i]
                count += r - l
 
                # checking for more possible solutions
                r -= 1
 
            else:
 
                # if not possible check for
                # higher values of arr[l]
                l += 1
    print("No of possible solutions: ", count)
 
 
# Driver Code
if __name__ == '__main__':
 
    A = [10, 21, 22, 100, 101, 200, 300]
 
    # Function call
    CountTriangles(A)
 
# This code is contributed by PrinciRaj1992

Time complexity: quadratic
Space complexity: constant
# Python program to merge
# two sorted arrays
# with O(1) extra space.
 
# Merge ar1[] and ar2[]
# with O(1) extra space
def merge(ar1, ar2, m, n):
 
    # Iterate through all
    # elements of ar2[] starting from
    # the last element
    for i in range(n-1, -1, -1):
     
        # Find the smallest element
        # greater than ar2[i]. Move all
        # elements one position ahead
        # till the smallest greater
        # element is not found
        last = ar1[m-1]
        j=m-2
        while(j >= 0 and ar1[j] > ar2[i]):
            ar1[j+1] = ar1[j]
            j-=1
  
        # If there was a greater element
        if (last > ar2[i]):
         
            ar1[j+1] = ar2[i]
            ar2[i] = last
  
# Driver program
 
ar1 = [1, 5, 9, 10, 15, 20]
ar2 = [2, 3, 8, 13]
m = len(ar1)
n = len(ar2)
 
merge(ar1, ar2, m, n)
  
print("After Merging \nFirst Array:", end="")
for i in range(m):
    print(ar1[i] , " ", end="")
 
print("\nSecond Array: ", end="")
for i in range(n):
    print(ar2[i] , " ", end="")
 
# This code is contributed
# by Anant Agarwal.

Time complexity: quadratic
Space complexity: constant
# Python program for the above approach
arr1 = [1, 5, 9, 10, 15, 20]
arr2 = [2, 3, 8, 13]
 
# Function to merge two arrays
 
 
def merge(n, m):
    i = 0
    j = 0
    k = n - 1
    while (i <= k and j < m):
        if (arr1[i] < arr2[j]):
            i += 1
        else:
            temp = arr2[j]
            arr2[j] = arr1[k]
            arr1[k] = temp
            j += 1
            k -= 1
 
    arr1.sort()
    arr2.sort()
 
 
# Driver code
if __name__ == '__main__':
    merge(len(arr1), len(arr2))
    print("After Merging \nFirst Array: ")
    print(','.join(str(x) for x in arr1))
    print("Second Array:  ")
    print(','.join(str(x) for x in arr2))
 
# This code is contributed by gauravrajput1

Time complexity: nlogn
Space complexity: constant
arr1 = [1, 5, 9, 10, 15, 20 ];
arr2 =[ 2, 3, 8, 13 ];
 
def merge(n, m):
    i = 0;
    temp = 0;
 
    # While loop till last element
    # of array 1(sorted)
    # is greater than first element
    # of array 2(sorted)
    while (arr1[n - 1] > arr2[0]):
        if (arr1[i] > arr2[0]):
 
            # Swap arr1[i] with first element
            # of arr2 and sorting the updated
            # arr2(arr1 is already sorted)
            # swap(arr1[i],arr2[0]);
            temp = arr1[i];
            arr1[i] = arr2[0];
            arr2[0] = temp;
            arr2.sort();
         
        i+=1;
     
 
 
# Driver code
if __name__ == '__main__':
    merge(len(arr1), len(arr2));
 
    print("After Merging \nFirst Array: ");
    print((arr1));
 
    print("Second Array:  ");
    print((arr2));
 
 
# This code contributed by gauravrajput1

Time complexity: linear
Space complexity: constant
# Python program to merge
# two sorted arrays
# with O(1) extra space.
 
# Merge ar1[] and ar2[]
# with O(1) extra space
 
 
def rotate(a, n, idx):
    for i in range((int)(idx/2)):
        a[i], a[idx-1-i] = a[idx-1-i], a[i]
    for i in range(idx, (int)((n+idx)/2)):
        a[i], a[n-1-(i-idx)] = a[n-1-(i-idx)], a[i]
    for i in range((int)(n/2)):
        a[i], a[n-1-i] = a[n-1-i], a[i]
 
 
def sol(a1, a2, n, m):
    l = 0
    h = n-1
    idx = 0
    while (l <= h):
        c1 = (int)((l+h)/2)
        c2 = n-c1-1
        l1 = a1[c1]
        l2 = a2[c2-1]
        r1 = sys.maxint if c1 == n-1 else a1[c1+1]
        r2 = sys.maxint if c2 == m else a2[c2]
        if l1 > r2:
            h = c1-1
            if h == -1:
                idx = 0
        elif l2 > r1:
            l = c1+1
            if l == n-1:
                idx = n
        else:
            idx = c1+1
            break
    for i in range(idx, n):
        a1[i], a2[i-idx] = a2[i-idx], a1[i]
 
    a1.sort()
    a2.sort()
 
 
def merge(a1, a2, n, m):
    if n > m:
        sol(a2, a1, m, n)
        rotate(a1, n, n-m)
        for i in range(m):
            a1[i], a2[i] = a2[i], a1[i]
    else:
        sol(a1, a2, n, m)
# Driver program
 
 
ar1 = [1, 5, 9, 10, 15, 20]
ar2 = [2, 3, 8, 13]
m = len(ar1)
n = len(ar2)
 
merge(ar1, ar2, m, n)
 
print("After Merging \nFirst Array:", end="")
for i in range(m):
    print(ar1[i], " ", end="")
print("\nSecond Array: ", end="")
for i in range(n):
    print(ar2[i], " ", end="")
# This code is contributed
# by Aditya Anand.

Time complexity: nlogn
Space complexity: constant
# code contributed by mahee96
 
# "Insertion sort of list 2 with swaps from list 1"
#
# swap elements to get list 1 correctly, meanwhile
# place the swapped item in correct position of list 2
# eventually list 2 is also sorted
# Time = O(m*n) or O(n*m)
# AUX = O(1)
def merge(arr1, arr2):
    x = arr1; y = arr2
    end = len(arr1)
    i = 0
    while(i < end):                 # O(m) or O(n)
        if(x[i] > y[0]):
            swap(x,y,i,0)
            insert(y,0)             # O(n) or O(m) number of shifts
        i+=1
 
# O(n):
def insert(y, i):
    orig = y[i]
    i+=1
    while (i<len(y) and y[i]<orig):
        y[i-1] = y[i]
        i+=1
    y[i-1] = orig
 
def swap(x,y,i,j):
    temp = x[i]
    x[i] = y[j]
    y[j] = temp
 
def test():
    c1 = [2, 3, 8, 13]
    c2 = [1, 5, 9, 10, 15, 20 ]
    for _ in range(2):
        merge(c1,c2)
        print(c1,c2)
        c1, c2 = c2, c1
 
test()

Time complexity: quadratic
Space complexity: constant
# A Python program to find the to
# calculate the product of max
# element of first array and min
# element of second array
 
# Function to calculate the product
def minmaxProduct(arr1, arr2, n1, n2):
 
    # Sort the arrays to find the
    # maximum and minimum elements
    # in given arrays
    arr1.sort()
    arr2.sort()
 
    # Return product of maximum
    # and minimum.
    return arr1[n1 - 1] * arr2[0]
 
# Driver Program
arr1 = [10, 2, 3, 6, 4, 1]
arr2 = [5, 1, 4, 2, 6, 9]
n1 = len(arr1)
n2 = len(arr2)
print(minmaxProduct(arr1, arr2, n1, n2))
 
# This code is contributed by Shrikant13.

Time complexity: nlogn
Space complexity: constant
# Python3 program to find the to
# calculate the product of
# max element of first array
# and min element of second array
 
# Function to calculate the product
def minMaxProduct(arr1, arr2,
                  n1, n2) :
 
    # Initialize max of first array
    max = arr1[0]
 
    # initialize min of second array
    min = arr2[0]
     
    i = 1
    while (i < n1 and i < n2) :
     
        # To find the maximum
        # element in first array
        if (arr1[i] > max) :
            max = arr1[i]
 
        # To find the minimum
        # element in second array
        if (arr2[i] < min) :
            min = arr2[i]
         
        i += 1
 
    # Process remaining elements
    while (i < n1) :
     
        if (arr1[i] > max) :
            max = arr1[i]
            i += 1
     
    while (i < n2):
     
        if (arr2[i] < min) :
            min = arr2[i]
            i += 1
 
    return max * min
 
# Driver code
arr1 = [10, 2, 3, 6, 4, 1 ]
arr2 = [5, 1, 4, 2, 6, 9 ]
n1 = len(arr1)
n2 = len(arr1)
print(minMaxProduct(arr1, arr2, n1, n2))
 
# This code is contributed by Smitha

Time complexity: linear
Space complexity: constant
# python 3  program to implement
# binary search in sorted array
 
 
def binarySearch(arr, low, high, key):
 
    mid = (low + high)/2
 
    if (key == arr[int(mid)]):
        return mid
 
    if (key > arr[int(mid)]):
        return binarySearch(arr,
                            (mid + 1), high, key)
 
    if (key < arr[int(mid)]):
        return binarySearch(arr, low, (mid-1), key)
 
    return 0
 
 
# Driver code
if __name__ == "__main__":
    # Let us search 3 in below array
    arr = [5, 6, 7, 8, 9, 10]
    n = len(arr)
    key = 10
 
    # Function call
    print("Index:", int(binarySearch(arr, 0, n-1, key)))
 
# This code is contributed by
# Smitha Dinesh Semwal

Time complexity: logn
Space complexity: logn
# Python3 program to implement insert
# operation in an sorted array.
 
# Inserts a key in arr[] of given capacity.
# n is current size of arr[]. This function
# returns n+1 if insertion is successful, else n.
 
 
def insertSorted(arr, n, key, capacity):
 
    # Cannot insert more elements if n is
    # already more than or equal to capacity
    if (n >= capacity):
        return n
 
    i = n - 1
    while i >= 0 and arr[i] > key:
        arr[i + 1] = arr[i]
        i -= 1
 
    arr[i + 1] = key
 
    return (n + 1)
 
 
# Driver Code
if __name__ == "__main__":
    arr = [12, 16, 20, 40, 50, 70]
 
    for i in range(20):
        arr.append(0)
 
    capacity = len(arr)
    n = 6
    key = 26
 
    print("Before Insertion: ", end=" ")
    for i in range(n):
        print(arr[i], end=" ")
 
    # Function call
    n = insertSorted(arr, n, key, capacity)
 
    print("\nAfter Insertion: ", end="")
    for i in range(n):
        print(arr[i], end=" ")
 
# This code is contributed by Mohit Kumar

Time complexity: linear
Space complexity: constant