Convert an array to reduced form | Set 1 ( Simple and Hashing ) | Python3 program to convert an array in reduced form ; Create a temp array and copy contents of arr [ ] to temp ; Sort temp array ; create a map ; One by one insert elements of sorted temp [ ] and assign them values from 0 to n - 1 ; Convert array by taking positions from umap ; Driver Code
def convert ( arr , n ) : NEW_LINE INDENT temp = [ arr [ i ] for i in range ( n ) ] NEW_LINE temp . sort ( ) NEW_LINE umap = { } NEW_LINE val = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT umap [ temp [ i ] ] = val NEW_LINE val += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = umap [ arr [ i ] ] NEW_LINE DEDENT DEDENT def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 20 , 15 , 12 , 11 , 50 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ Array ▁ is ▁ " ) NEW_LINE printArr ( arr , n ) NEW_LINE convert ( arr , n ) NEW_LINE print ( " Converted Array is   " ) NEW_LINE printArr ( arr , n ) NEW_LINE DEDENT