Rearrange array to make it non | Python3 program for above approach ; Function to check if the array can be made non - decreasing ; Iterate till N ; Find the minimum element ; Sort the array ; Iterate till N ; Check if the element is at its correct position ; Return the answer ; Driver Code ; Print the answer
import sys NEW_LINE def check ( a , n ) : NEW_LINE INDENT b = [ None ] * n NEW_LINE minElement = sys . maxsize NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT b [ i ] = a [ i ] NEW_LINE minElement = min ( minElement , a [ i ] ) NEW_LINE DEDENT b . sort ( ) NEW_LINE k = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( a [ i ] != b [ i ] ) and ( a [ i ] % minElement != 0 ) ) : NEW_LINE INDENT k = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if k == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 3 , 6 , 6 , 2 , 9 ] NEW_LINE n = len ( a ) NEW_LINE if check ( a , n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT