Check if it is possible to sort an array with conditional swapping of elements at distance K | C # implementation to check if it is possible to sort an array with conditional swapping of elements at distance K ; Function for finding if it possible to obtain sorted array or not ; Iterate over all elements until K ; Store elements as multiples of K ; Sort the elements ; Put elements in their required position ; Check if the array becomes sorted or not ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static bool fun ( int [ ] arr , int n , int k ) { List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { v . Add ( arr [ j ] ) ; } v . Sort ( ) ; int x = 0 ; for ( int j = i ; j < n ; j += k ) { arr [ j ] = v [ x ] ; x ++ ; } v . Clear ( ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 3 , 7 , 6 } ; int K = 2 ; int n = arr . Length ; if ( fun ( arr , n , K ) ) { Console . WriteLine ( " yes " ) ; } else { Console . WriteLine ( " no " ) ; } } }