- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given an integer type array as ‘int arr[]’ and an integer type variable as ‘x’. The task is to rearrange all the elements of an array in such a manner that they will be divisible by a given integer value ‘x’ and the arrangement should be in an increasing order.

**Input **− int arr[] = {4,24, 3, 5, 7, 22, 12, 10}, int x = 2

**Output **− Rearrangement of all elements of array which are multiples of x 2 in decreasing order is: 4 10 3 5 7 12 22 24

**Explanation **−we are given an integer type array containing values as {4,24, 3, 5, 7, 22, 12, 10} and x with the value as 2. Now firstly we will check all the elements from an array which are divisible by 2 i.e. 4, 24, 22, 12, 10. Now we will arrange all the elements in an increasing orderi.e. 4, 10, 3, 5, 7, 12, 22, 24 which is the final output.

**Input **− nt arr[] = {4,24, 3, 5, 7, 22, 12, 10}, int x = 3

**Output **− Rearrangement of all elements of array which are multiples of x 3 in decreasing order is: 4 3 12 5 7 22 24 10

**Explanation **− we are given an integer type array containing values as {4,24, 3, 5, 7, 22, 12, 10} and x with the value as 3. Now firstly we will check all the elements from an array which are
divisible by 3 i.e. 3, 24, 12. Now we will arrange all the elements in an increasing order i.e. 4, 3, 12, 5, 7, 22, 24, 10 which is the final output.

Declare an integer type array. Calculate the size of an array and store it in a variable named size. Declare an integer type variable ‘x’ against which we need to rearrange an array.

Pass the data to the function Rearrange_Elements(arr, size, x)

Inside the function Rearrange_Elements(arr, size, x)

Create a variable as vec of type vector storing integer type values.

Start loop FOR from i to 0 till i less than size. Inside the loop, check IF arr[i] % x = 0 then push arr[i] to the vec

Sort an array using C++ STL sort method where we will pass begin() and end() as a parameter to the function.

Start loop FOR from i to 0 till i less than size. Check IF arr[i] % x = 0 then set arr[i] to vec[j++].

Print an array using the for loop by traversing it from the first element of an array till the last available element.

#include <bits/stdc++.h> using namespace std; void Rearrange_Elements(int arr[], int size, int x){ vector<int> vec; int j = 0; for(int i = 0; i < size; i++){ if(arr[i] % x == 0){ vec.push_back(arr[i]); } } sort(vec.begin(), vec.end()); for (int i = 0; i < size; i++){ if(arr[i] % x == 0){ arr[i] = vec[j++]; } } cout<<"Rearrangement of all elements of array which are multiples of x "<<x<<" in decreasing order is: "; for(int i = 0; i < size; i++){ cout << arr[i] << " "; } } int main(){ int arr[] = {4,24, 3, 5, 7, 22, 12, 10}; int x = 2; int size = sizeof(arr) / sizeof(arr[0]); Rearrange_Elements(arr, size, x); return 0; }

If we run the above code it will generate the following Output

Rearrangement of all elements of array which are multiples of x 2 in decreasing order is: 4 10 3 5 7 12 22 24

- Related Questions & Answers
- Python program to print elements which are multiples of elements given in a list
- Maximum sum of increasing order elements from n arrays in C++ program
- Program to filter all values which are greater than x in an array
- Count all prefixes of the given binary array which are divisible by x in C++
- C# Program to order array elements
- C# Program to order array elements in descending order
- Print array elements in alternatively increasing and decreasing order in C++
- Maximum sum of increasing order elements from n arrays in C++
- Program to sort array by increasing frequency of elements in Python
- Rearrange an array to minimize sum of product of consecutive pair elements in C++
- Array of multiples - JavaScript
- Python Program to test whether the length of rows are in increasing order
- Sort elements of the array that occurs in between multiples of K in C++
- How to check whether the elements of a vector are arranged in an increasing order or decreasing order?
- Find array elements that are out of order in JavaScript

Advertisements