Home  >  Article  >  Backend Development  >  C++ program to rearrange all elements that are multiples of x in an array in increasing order

C++ program to rearrange all elements that are multiples of x in an array in increasing order

王林
王林forward
2023-08-30 20:25:061176browse

C++ program to rearrange all elements that are multiples of x in an array in increasing order

We have an array of integer type `int arr[]` and a variable of type integer `x`. The task is to rearrange all the elements of the array so that they are divisible by the given integer value `x` and the order should be increasing.

Let us look at the various input and output situations of this problem:

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

Output - The rearrangement result of all elements in the array that can be divided by x=2 is: 4 10 3 5 7 12 22 24

Explanation - We have an array of integer type containing the values ​​{4,24, 3, 5, 7, 22, 12, 10}, and x with the value 2. Now first we will check all the elements in the array that are divisible by 2 i.e. 4, 24, 22, 12, 10. Then we will arrange all the elements in increasing order i.e. 4, 10, 3, 5, 7, 12, 22, 24 and this is the final output.

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

Output - The rearrangement result of all elements in the array that can be divided by x=3 is: 4 3 12 5 7 22 24 10

Explanation - We have an integer type Array containing the values ​​{4,24, 3, 5, 7, 22, 12, 10} and x with the value 3. Now first we will check all the elements in the array that are divisible by 3 i.e. 4, 24, 12. Then we will arrange all the elements in increasing order i.e. 4, 3, 12, 5, 7, 22, 24, 10 and this is the final output.

The method used in the following program is as follows

  • Declare an array of integer type. Calculate the size of the array and store it in a variable called size. Declare a variable 'x' of type integer for rearranging the array.

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

  • In function Rearrange_Elements(arr, size, x)

    • Create a vector type variable named vec to store integer type values.

    • Loop from i to 0 until i is less than size. Inside the loop, check IF arr[i] % x = 0, then push arr[i] into the vec

    • Sort the array using the sort method of C STL, where we Pass begin() and end() as parameters to the function.

    • Loop from i to 0 until i is less than size. Check IF arr[i] % x = 0, then set arr[i] to vec[j].

    • Use a for loop to print the array, traversing from the first element of the array to the last available element.

Example

#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;
}

Output

If we run the above code, the following output will be generated

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

The above is the detailed content of C++ program to rearrange all elements that are multiples of x in an array in increasing order. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete