- 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

# 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

- Related Questions & Answers
- Find N digits number which is divisible by D in C++
- Sum which is divisible by n in JavaScript
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Maximum and Minimum element of a linked list which is divisible by a given number k in C++
- Maximize the number of sum pairs which are divisible by K in C++
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- Is it true that Anorexia is also a mental disorder?
- Find if a number is divisible by every number in a list in C++
- Check if a number is divisible by all prime divisors of another number in C++
- Count the number of elements in an array which are divisible by k in C++
- Check if a large number is divisible by 20 in C++
- Generating a random number that is divisible by n in JavaScript
- Finding a pair that is divisible by some number in JavaScript
- Number is divisible by 29 or not in C++
- Check if any permutation of a large number is divisible by 8 in Python