Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array | C # implementation of the above approach ; Function to check if it is possible to sort array or not ; Store the smallest element ; Copy the original array ; Iterate over the given array ; Update smallest element ; Copy elements of [ ] arr to array [ ] B ; Sort original array ; Iterate over the given array ; If the i - th element is not in its sorted place ; Not possible to swap ; Driver Code ; Given array ; Function Call
using System ; class GFG { static void isPossible ( int [ ] arr , int N ) { int mn = int . MaxValue ; int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { mn = Math . Min ( mn , arr [ i ] ) ; B [ i ] = arr [ i ] ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != B [ i ] ) { if ( B [ i ] % mn != 0 ) { Console . Write ( " No " ) ; return ; } } } Console . Write ( " Yes " ) ; return ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] arr = { 4 , 3 , 6 , 6 , 2 , 9 } ; isPossible ( arr , N ) ; } }