Sort an array by left shifting digits of array elements | Function to check if an array is sorted in increasing order or not ; Traverse the array ; Function to sort the array by left shifting digits of array elements ; Stores previous array element ; Traverse the array arr [ ] ; Stores current element ; Stores current element in string format ; Left - shift digits of current element in all possible ways ; Left - shift digits of current element by idx ; If temp greater than or equal to prev and temp less than optEle ; Update arr [ i ] ; Update prev ; If arr is in increasing order ; Otherwise ; Driver Code
def isIncreasing ( arr ) : NEW_LINE INDENT for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT if arr [ i ] > arr [ i + 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def sortArr ( arr ) : NEW_LINE INDENT prev = - 1 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT optEle = arr [ i ] NEW_LINE strEle = str ( arr [ i ] ) NEW_LINE for idx in range ( len ( strEle ) ) : NEW_LINE INDENT temp = int ( strEle [ idx : ] + strEle [ : idx ] ) NEW_LINE if temp >= prev and temp < optEle : NEW_LINE INDENT optEle = temp NEW_LINE DEDENT DEDENT arr [ i ] = optEle NEW_LINE prev = arr [ i ] NEW_LINE DEDENT if isIncreasing ( arr ) : NEW_LINE INDENT return arr NEW_LINE DEDENT else : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 511 , 321 , 323 , 432 , 433 ] NEW_LINE res = sortArr ( arr ) NEW_LINE for i in res : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT