Rearrange all elements of array which are multiples of x in increasing order | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortMultiples ( int arr [ ] , int n , int x ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % x == 0 ) v . push_back ( arr [ i ] ) ; sort ( v . begin ( ) , v . end ( ) ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) arr [ i ] = v [ j ++ ] ; } } int main ( ) { int arr [ ] = { 125 , 3 , 15 , 6 , 100 , 5 } ; int x = 5 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortMultiples ( arr , n , x ) ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } return 0 ; }