Rearrange array to make it non | 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 ; Prlet the answer
function check ( a , n ) { let b = new Array ( n ) . fill ( 0 ) ; let minElement = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { b [ i ] = a [ i ] ; minElement = Math . min ( minElement , a [ i ] ) ; } b . sort ( ) ; let k = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] && a [ i ] % minElement != 0 ) { k = 0 ; break ; } } return k == 1 ? true : false ; } let a = [ 4 , 3 , 6 , 6 , 2 , 9 ] ; let n = a . length ; if ( check ( a , n ) == true ) { document . write ( " " ) ; } else { document . write ( " " ) ; }