Rearrange array to make it non | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n ) { int b [ n ] ; int minElement = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = a [ i ] ; minElement = min ( minElement , a [ i ] ) ; } sort ( b , b + n ) ; int k = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] && a [ i ] % minElement != 0 ) { k = 0 ; break ; } } return k == 1 ? true : false ; } int main ( ) { int a [ ] = { 4 , 3 , 6 , 6 , 2 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( check ( a , n ) == true ) cout << " Yes ▁ STRNEWLINE " ; else cout << " No ▁ STRNEWLINE " ; return 0 ; }