# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Traverse the array and check if the number is multiple of x. If it is, store it in a vector.
2. Then, sort the vector in decreasing order.
3. 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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to sort all the ` `// multiples of x from the ` `// array in decreasing order ` `void` `sortMultiples(``int` `arr[], ``int` `n, ``int` `x) ` `{ ` `    ``vector<``int``> v; ` ` `  `    ``// Insert all multiples of x to a vector ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``if` `(arr[i] % x == 0) ` `            ``v.push_back(arr[i]); ` ` `  `    ``// Sort the vector in descending ` `    ``sort(v.begin(), v.end(), std::greater<``int``>()); ` ` `  `    ``int` `j = 0; ` ` `  `    ``// update the array elements ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(arr[i] % x == 0) ` `            ``arr[i] = v[j++]; ` `    ``} ` `} ` ` `  `// Utility function to print the array ` `void` `printArray(``int` `arr[], ``int` `N) ` `{ ` `    ``// Print the array ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``cout << arr[i] << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 125, 3, 15, 6, 100, 5 }; ` `    ``int` `x = 5; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``sortMultiples(arr, n, x); ` ` `  `    ``printArray(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Function to sort all the ` `    ``// multiples of x from the ` `    ``// array in decreasing order ` `    ``static` `void` `sortMultiples(``int` `arr[], ``int` `n, ``int` `x)  ` `    ``{ ` `        ``Vector v = ``new` `Vector(); ` ` `  `        ``// Insert all multiples of x to a vector ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``if` `(arr[i] % x == ``0``)  ` `            ``{ ` `                ``v.add(arr[i]); ` `            ``} ` `        ``} ` ` `  `        ``// Sort the vector in descending ` `        ``Collections.sort(v, Collections.reverseOrder()); ` ` `  `        ``int` `j = ``0``; ` ` `  `        ``// update the array elements ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``if` `(arr[i] % x == ``0``)  ` `            ``{ ` `                ``arr[i] = v.get(j++); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Utility function to print the array ` `    ``static` `void` `printArray(``int` `arr[], ``int` `N) ` `    ``{ ` `        ``// Print the array ` `        ``for` `(``int` `i = ``0``; i < N; i++) ` `        ``{ ` `            ``System.out.print(arr[i] + ``" "``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``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); ` `    ``} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to sort all the  ` `# multiples of x from the  ` `# array in decreasing order  ` `def` `sortMultiples(arr, n, x) : ` `    ``v ``=` `[]  ` ` `  `    ``# Insert all multiples of x  ` `    ``# to a vector  ` `    ``for` `i ``in` `range``(n) :  ` `        ``if` `(arr[i] ``%` `x ``=``=` `0``) : ` `            ``v.append(arr[i])  ` ` `  `    ``# Sort the vector in descending  ` `    ``v.sort(reverse ``=` `True``) ` `    ``j ``=` `0` ` `  `    ``# update the array elements  ` `    ``for` `i ``in` `range``(n) :  ` `        ``if` `(arr[i] ``%` `x ``=``=` `0``) : ` `            ``arr[i] ``=` `v[j] ` `            ``j ``+``=` `1` `             `  `# Utility function to print the array  ` `def` `printArray(arr, N) : ` ` `  `    ``# Print the array  ` `    ``for` `i ``in` `range``(N) :  ` `        ``print``(arr[i], end ``=` `" "``) ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``arr``=` `[ ``125``, ``3``, ``15``, ``6``, ``100``, ``5` `]  ` `    ``x ``=` `5` `    ``n ``=` `len``(arr) ` `     `  `    ``sortMultiples(arr, n, x) ` ` `  `    ``printArray(arr, n)  ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Function to sort all the ` `    ``// multiples of x from the ` `    ``// array in decreasing order ` `    ``static` `void` `sortMultiples(``int` `[]arr, ``int` `n, ``int` `x)  ` `    ``{ ` `        ``List<``int``> v = ``new` `List<``int``>(); ` ` `  `        ``// Insert all multiples of x to a vector ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` `            ``if` `(arr[i] % x == 0)  ` `            ``{ ` `                ``v.Add(arr[i]); ` `            ``} ` `        ``} ` ` `  `        ``// Sort the vector in descending ` `        ``v.Sort(); ` `        ``v.Reverse(); ` ` `  `        ``int` `j = 0; ` ` `  `        ``// update the array elements ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``if` `(arr[i] % x == 0)  ` `            ``{ ` `                ``arr[i] = v[j++]; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Utility function to print the array ` `    ``static` `void` `printArray(``int` `[]arr, ``int` `N) ` `    ``{ ` `        ``// Print the array ` `        ``for` `(``int` `i = 0; i < N; i++) ` `        ``{ ` `            ``Console.Write(arr[i] + ``" "``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``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); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## PHP

 ` `

Output:

```125 3 100 6 15 5
```

Time Complexity : O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.