Find if array can be sorted by swaps limited to multiples of k |  ; CheckSort function To check if array can be sorted ; sortarr is sorted array of arr ; If k = 1 then ( always possible to sort ) swapping can easily give sorted array ; Comparing sortarray with array ; Element at index j must be in j = i + l * k form where i = 0 , 1 , 2 , 3. . . where l = 0 , 1 , 2 , 3 , . . n - 1 ; If element is present then swapped ; If element of sorted array does not found in its sequence then flag remain zero that means arr can not be sort after swapping ; If flag is 0 Not possible else Possible ; Driver code ; Size of step ; Array initialized ; Length of arr ; Calling function
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void CheckSort ( ArrayList arr , int k , int n ) { ArrayList sortarr = new ArrayList ( arr ) ; sortarr . Sort ( ) ; if ( k == 1 ) Console . Write ( " yes " ) ; else { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { flag = 0 ; for ( int j = i ; j < n ; j += k ) { if ( ( int ) sortarr [ i ] == ( int ) arr [ j ] ) { int tmp = ( int ) arr [ i ] ; arr [ i ] = ( int ) arr [ j ] ; arr [ j ] = tmp ; flag = 1 ; break ; } if ( j + k >= n ) { break ; } } if ( flag == 0 ) { break ; } } if ( flag == 0 ) Console . Write ( " Not ▁ possible ▁ to ▁ sort " ) ; else Console . Write ( " Possible ▁ to ▁ sort " ) ; } } public static void Main ( string [ ] args ) { int k = 3 ; ArrayList arr = new ArrayList ( ) { 1 , 5 , 6 , 9 , 2 , 3 , 5 , 9 } ; int n = arr . Count ; CheckSort ( arr , k , n ) ; } }