Rearrange all elements of array which are multiples of x in increasing order | Function to sort all the multiples of x from the array in ascending order ; Insert all multiples of 5 to a vector ; Sort the vector ; update the array elements ; Driver code ; Print the result
function sortMultiples ( arr , n , x ) { var v = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) { v . push ( arr [ i ] ) ; } } v . sort ( ( a , b ) => a - b ) ; var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) arr [ i ] = v [ j ++ ] ; } } var arr = [ 125 , 3 , 15 , 6 , 100 , 5 ] ; var x = 5 ; var n = arr . length ; sortMultiples ( arr , n , x ) ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }