Given an array of integers ‘arr’ and a number x, the task is to sort all the elements which are multiples of x of the array in ascending order in their relative positions i.e. other positions of the other elements must not be affected.
Examples:
Input: arr[] = {10, 5, 8, 2, 15}, x = 5
Output: 5 10 8 2 15
We rearrange all multiples of 5 in increasing order, keeping other elements same.
Input: arr[] = {100, 12, 25, 50, 5}, x = 5
Output: 5 12 25 50 100
Approach:
- Traverse the array and check if the number is multiple of x. If it is, store it in a vector.
- Then, sort the vector in ascending order.
- Again traverse the array and replace the elements which are multiples of 5 with the vector elements one by one.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.Collections;
import java.util.Vector;
class GFG {
static void sortMultiples( int arr[], int n, int x) {
Vector<Integer> v = new Vector<Integer>();
for ( int i = 0 ; i < n; i++) {
if (arr[i] % x == 0 ) {
v.add(arr[i]);
}
}
Collections.sort(v);
int j = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % x == 0 ) {
arr[i] = v.get(j++);
}
}
}
public static void main(String[] args) {
int arr[] = { 125 , 3 , 15 , 6 , 100 , 5 };
int x = 5 ;
int n = arr.length;
sortMultiples(arr, n, x);
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i]+ " " );
}
}
}
|
Python3
def sortMultiples(arr, n, x):
v = []
for i in range ( 0 , n, 1 ):
if (arr[i] % x = = 0 ):
v.append(arr[i])
v.sort(reverse = False )
j = 0
for i in range ( 0 , n, 1 ):
if (arr[i] % x = = 0 ):
arr[i] = v[j]
j + = 1
if __name__ = = '__main__' :
arr = [ 125 , 3 , 15 , 6 , 100 , 5 ]
x = 5
n = len (arr)
sortMultiples(arr, n, x)
for i in range ( 0 , n, 1 ):
print (arr[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void sortMultiples( int []arr,
int n, int x)
{
List< int > v = new List< int >();
int i;
for (i = 0; i < n; i++)
if (arr[i] % x == 0)
v.Add(arr[i]);
v.Sort();
int j = 0;
for (i = 0; i < n; i++)
{
if (arr[i] % x == 0)
arr[i] = v[j++];
}
}
public static void Main()
{
int []arr = {125, 3, 15, 6, 100, 5};
int x = 5;
int n = arr.Length;
sortMultiples(arr, n, x);
for ( int i = 0; i < n; i++)
{
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
<script>
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] + " " );
}
</script>
|
Complexity Analysis:
- Time Complexity: O(N*logN), as we are using inbuilt sort function which cost the afore mentioned time.
- Auxiliary Space: O(N), as we are using extra space for array/vector v.