Rearrange all elements of array which are multiples of x in increasing order
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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to sort all the // multiples of x from the // array in ascending order void sortMultiples(int arr[], int n, int x) { vector<int> v; // Insert all multiples of 5 to a vector for (int i = 0; i < n; i++) if (arr[i] % x == 0) v.push_back(arr[i]); // Sort the vector sort(v.begin(), v.end()); int j = 0; // update the array elements for (int i = 0; i < n; i++) { if (arr[i] % x == 0) arr[i] = v[j++]; } } // Driver code int main() { int arr[] = { 125, 3, 15, 6, 100, 5 }; int x = 5; int n = sizeof(arr) / sizeof(arr[0]); sortMultiples(arr, n, x); // Print the result for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; } |
Java
import java.util.Collections; import java.util.Vector; // Java implementation of the approach class GFG { // Function to sort all the // multiples of x from the // array in ascending order static void sortMultiples(int arr[], int n, int x) { Vector<Integer> v = new Vector<Integer>(); // Insert all multiples of 5 to a vector for (int i = 0; i < n; i++) { if (arr[i] % x == 0) { v.add(arr[i]); } } // Sort the vector Collections.sort(v); //sort(v.begin(), v.end()); int j = 0; // update the array elements for (int i = 0; i < n; i++) { if (arr[i] % x == 0) { arr[i] = v.get(j++); } } } // 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); // Print the result for (int i = 0; i < n; i++) { System.out.print(arr[i]+" "); } } } // This code is contributed by Rajput-Ji |
Python3
# Python 3 implementation of the approach # Function to sort all the multiples of x # from the array in ascending order def sortMultiples(arr, n, x): v = [] # Insert all multiples of 5 to a vector for i in range(0, n, 1): if (arr[i] % x == 0): v.append(arr[i]) # Sort the vector v.sort(reverse=False) j = 0 # update the array elements for i in range(0, n, 1): if (arr[i] % x == 0): arr[i] = v[j] j += 1 # Driver code if __name__ == '__main__': arr = [ 125, 3, 15, 6, 100, 5] x = 5 n = len(arr) sortMultiples(arr, n, x) # Print the result for i in range(0, n, 1): print(arr[i], end = " ") # This code is contributed by # Surendra _Gangwar |
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 ascending order static void sortMultiples(int []arr, int n, int x) { List<int> v = new List<int>(); int i; // Insert all multiples of 5 to a vector for (i = 0; i < n; i++) if (arr[i] % x == 0) v.Add(arr[i]); // Sort the vector v.Sort(); int j = 0; // update the array elements for (i = 0; i < n; i++) { if (arr[i] % x == 0) arr[i] = v[j++]; } } // 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); // Print the result for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); } } } // This code is cntributed by // Shivi_Aggarwal |
5 3 15 6 100 125
Recommended Posts:
- Rearrange all elements of array which are multiples of x in decreasing order
- Print array elements in alternatively increasing and decreasing order
- Shortest path to traverse all the elements of a circular array in increasing order
- Sort even and odd placed elements in increasing order
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Maximum sum of increasing order elements from n arrays
- Minimum sum after subtracting multiples of k from the elements of the array
- Sort elements of the array that occurs in between multiples of K
- Sort only non-prime numbers of an array in increasing order
- Minimum increment operations to make the array in increasing order
- Divide array into increasing and decreasing subsequence without changing the order
- Rearrange an array to minimize sum of product of consecutive pair elements
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Sum of array elements that is first continuously increasing then decreasing
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
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.



