Sort elements by modulo with K | Utility function to prthe contents of an array ; Function to sort the array elements based on their modulo with K ; Create K empty vectors ; Update the vectors such that v [ i ] will contain all the elements that give remainder as i when divided by k ; Sorting all the vectors separately ; Replacing the elements in arr [ ] with the required modulo sorted elements ; Add all the elements of the current vector to the array ; Print the sorted array ; Driver code
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT def sortWithRemainder ( arr , n , k ) : NEW_LINE INDENT v = [ [ ] for i in range ( k ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT v [ arr [ i ] % k ] . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT v [ i ] . sort ( ) NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT for it in v [ i ] : NEW_LINE INDENT arr [ j ] = it NEW_LINE j += 1 NEW_LINE DEDENT DEDENT printArr ( arr , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 7 , 2 , 6 , 12 , 3 , 33 , 46 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE sortWithRemainder ( arr , n , k ) NEW_LINE DEDENT