Rearrangement of a number which is also divisible by it in C++


We are given an integer type number, let's say, number. The task is to rearrange number digit’s in such a manner that a number formed after rearrangement is also divisible by the given number i.e. ’number’.

Let us see various input output scenarios for this −

Input − int number = 100035

Output − Rearrangement of a number which is also divisible by it is: 300105

Explanation − we are given an integer number as ‘number’ i.e. 100035. Now, the task is to rearrange these given digits in such a manner that the formed number will be divisible by 100035. So, after rearranging the digits we got 300105 which is divisible by the number 100035.

Input − int number = 1000035

Output − Rearrangement of a number which is also divisible by it is: 3000105

Explanation − we are given an integer number as ‘number’ i.e. 1000035. Now, the task is to rearrange these given digits in such a manner that the formed number will be divisible by 1000035. So, after rearranging the digits we got 3000105 which is divisible by the number 1000035.

Input − int number = 28

Output − Rearrangement of a number which is also divisible by it is: -1

Explanation − we are given an integer number as ‘number’ i.e. 28. Now, the task is to rearrange these given digits in such a manner that the formed number will be divisible by 28. So after rearranging the digits we got 82 which isn’t divisible by the number 28 therefore the output is -1.

Approach used in the below program is as follows

  • Input a variable of integer type, let’s say, number and Pass the data to the function Rearrangement(number ).

  • Inside the function Rearrangement(number)

    • Create a variable of type vector which is storing integer type variables, let’s say, vec(10, 0).

    • Call the function total_count(number, vec) and pass number and vec as a parameter to the function.

    • Start loop FOR from i to 2 till i less than 10. Inside the loop, set temp to number * i.

    • Create a variable of type vector which is storing integer type variables, let’s say, vec_2(10, 0).

    • Call the function total_count(number, vec_2) and pass number and vec_2 as a parameter to the function.

    • Check IF equal(vec.begin(), vec.end(), vec_2.begin()) then return temp. ELSE, return -1.

  • Inside the function total_count(int number, vector<int> &vec_3)

    • Start loop WHILE number is 1. Inside the loop, set vec_3 as vec_3[number % 10]++ and number to number / 10.

Example

#include<bits/stdc++.h>
using namespace std;
void total_count(int number, vector<int> &vec_3){
   while(number){
      vec_3[number % 10]++;
      number = number / 10;
   }
}
int Rearrangement(int number){
   vector<int> vec(10, 0);
   total_count(number, vec);
   for(int i = 2; i < 10; i++){
      int temp = number * i;
      vector<int> vec_2(10, 0);
      total_count(temp, vec_2);
      if(equal(vec.begin(), vec.end(), vec_2.begin())){
         return temp;
      }
   }
   return -1;
}
int main(){
   int number = 100035;
   cout<<"Rearrangement of a number which is also divisible by it is: "<<Rearrangement(number);
   return 0;
}

Output

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

Rearrangement of a number which is also divisible by it is: 300105

Updated on: 02-Nov-2021

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements