Convert an array to reduced form | Set 3 ( Binary Search ) | Function to find the reduced form of the given array arr [ ] ; Stores the sorted form of the the given array arr [ ] ; Sort the array brr [ ] ; Traverse the given array arr [ ] ; Perform the Binary Search ; Calculate the value of mid ; Prthe current index and break ; Update the value of l ; Update the value of r ; Driver Code
def convert ( arr , n ) : NEW_LINE INDENT brr = [ i for i in arr ] NEW_LINE brr = sorted ( brr ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT l , r , mid = 0 , n - 1 , 0 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( brr [ mid ] == arr [ i ] ) : NEW_LINE INDENT print ( mid , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT elif ( brr [ mid ] < arr [ i ] ) : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 20 , 15 , 12 , 11 , 50 ] NEW_LINE N = len ( arr ) NEW_LINE convert ( arr , N ) NEW_LINE DEDENT