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
def fun ( arr , n , k ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( i , n , k ) : NEW_LINE INDENT v . append ( arr [ j ] ) ; NEW_LINE DEDENT v . sort ( ) ; NEW_LINE x = 0 NEW_LINE for j in range ( i , n , k ) : NEW_LINE INDENT arr [ j ] = v [ x ] ; NEW_LINE x += 1 NEW_LINE DEDENT v = [ ] NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 4 , 2 , 3 , 7 , 6 ] NEW_LINE K = 2 ; NEW_LINE n = len ( arr ) NEW_LINE if ( fun ( arr , n , K ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT