Rearrange all elements of array which are multiples of x in decreasing order
Given an array of integers arr[] and an integer x, the task is to sort all the elements of the array which are multiples of x in decreasing order in their relative positions i.e. positions of the other elements must not be affected.
Examples:
Input: arr[] = {10, 5, 8, 2, 15}, x = 5
Output: 15 10 8 2 5
We rearrange all multiples of 5 (i.e. 10, 5 and 15) in decreasing order in their relative positions, keeping other elements same.
Input: arr[] = {100, 12, 25, 50, 5}, x = 5
Output: 100 12 50 25 5
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 decreasing 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(), std::greater< int >());
int j = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % x == 0)
arr[i] = v[j++];
}
}
void printArray( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 125, 3, 15, 6, 100, 5 };
int x = 5;
int n = sizeof (arr) / sizeof (arr[0]);
sortMultiples(arr, n, x);
printArray(arr, n);
return 0;
}
|
Java
import java.util.*;
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, Collections.reverseOrder());
int j = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % x == 0 )
{
arr[i] = v.get(j++);
}
}
}
static void printArray( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
System.out.print(arr[i] + " " );
}
}
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);
printArray(arr, n);
}
}
|
Python3
def sortMultiples(arr, n, x) :
v = []
for i in range (n) :
if (arr[i] % x = = 0 ) :
v.append(arr[i])
v.sort(reverse = True )
j = 0
for i in range (n) :
if (arr[i] % x = = 0 ) :
arr[i] = v[j]
j + = 1
def printArray(arr, N) :
for i in range (N) :
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 125 , 3 , 15 , 6 , 100 , 5 ]
x = 5
n = len (arr)
sortMultiples(arr, n, x)
printArray(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void sortMultiples( int []arr, int n, int x)
{
List< int > v = new List< int >();
for ( int i = 0; i < n; i++)
{
if (arr[i] % x == 0)
{
v.Add(arr[i]);
}
}
v.Sort();
v.Reverse();
int j = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % x == 0)
{
arr[i] = v[j++];
}
}
}
static void printArray( int []arr, int N)
{
for ( int i = 0; i < N; i++)
{
Console.Write(arr[i] + " " );
}
}
public static void Main()
{
int []arr = {125, 3, 15, 6, 100, 5};
int x = 5;
int n = arr.Length;
sortMultiples(arr, n, x);
printArray(arr, n);
}
}
|
PHP
<?php
function sortMultiples(& $arr , $n , $x )
{
$v = array ();
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] % $x == 0)
array_push ( $v , $arr [ $i ]);
rsort( $v );
$j = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] % $x == 0)
$arr [ $i ] = $v [ $j ++];
}
}
function printArray( $arr , $N )
{
for ( $i = 0; $i < $N ; $i ++)
{
echo $arr [ $i ] . " " ;
}
}
$arr = array (125, 3, 15, 6, 100, 5 );
$x = 5;
$n = sizeof( $arr );
sortMultiples( $arr , $n , $x );
printArray( $arr , $n );
?>
|
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) => b - a);
var j = 0;
for ( var i = 0; i < n; i++)
{
if (arr[i] % x == 0)
arr[i] = v[j++];
}
}
function printArray(arr, N)
{
for ( var i = 0; i < N; i++)
{
document.write(arr[i] + " " );
}
}
var arr = [ 125, 3, 15, 6, 100, 5 ];
var x = 5;
var n = arr.length;
sortMultiples(arr, n, x);
printArray(arr, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(nlogn)
- Auxiliary Space: O(n)
Last Updated :
12 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...