Given a number n, we need to rearrange all its digits such that the new arrangement is divisible by n. Also, the new number should not be equal to x. If no such rearrangement is possible, print -1. Examples:
Input : n = 1035
Output : 3105
The result 3105 is divisible by
given n and has the same set of digits.
Input : n = 1782
Output : m = 7128
Simple Approach : Find all the permutation of given n and then check whether it is divisible by n or not also check that new permutation should not be equal to n. Efficient Approach : Let’s suppose that y is our result then y = m * n, also we know that y must be a rearrangement of digits of n so we can say now restrict m (the multiplier) as per given conditions. 1) y has the same number of digits as n has. So, m must be less than 10. 2) y must not be equal to n. So, m will be greater than 1. So we get the multiplier m in the range [2,9]. So we will find all the possible y and then check that should y has the same digits as n or not.
C++
#include<bits/stdc++.h>
using namespace std;
void storeDigitCounts( int n, vector< int > &hash)
{
while (n)
{
hash[n%10]++;
n /= 10;
}
}
int rearrange ( int n)
{
vector< int > hash_n(10, 0);
storeDigitCounts(n, hash_n);
for ( int mult=2; mult<10; mult++)
{
int curr = n * mult;
vector< int > hash_curr(10, 0);
storeDigitCounts(curr, hash_curr);
if (equal(hash_n.begin(), hash_n.end(),
hash_curr.begin()))
return curr;
}
return -1;
}
int main()
{
int n = 10035;
cout << rearrange(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int [] storeDigitCounts( int n)
{
int arr[] = new int [ 10 ];
for ( int i = 0 ; i < 10 ; i++)
arr[i] = 0 ;
while (n > 0 )
{
arr[n % 10 ]++;
n /= 10 ;
}
return arr;
}
static int rearrange ( int n)
{
int [] hash = storeDigitCounts(n);
for ( int mult= 2 ; mult< 10 ; mult++)
{
int curr = n*mult;
int [] hash_curr = storeDigitCounts(curr);
int ind;
for (ind = 0 ; ind < 10 ; ind++)
{
if (hash_curr[ind] != hash[ind])
break ;
}
if (ind == 10 ) return curr;
}
return - 1 ;
}
public static void main(String[] args)
{
int n = 10035 ;
System.out.println(rearrange(n));
}
}
|
Python3
def storeDigitCounts(n, Hash ):
while n > 0 :
Hash [n % 10 ] + = 1
n / / = 10
def rearrange(n):
hash_n = [ 0 ] * 10
storeDigitCounts(n, hash_n)
for mult in range ( 2 , 10 ):
curr = n * mult
hash_curr = [ 0 ] * 10
storeDigitCounts(curr, hash_curr)
if hash_n = = hash_curr:
return curr
return - 1
if __name__ = = "__main__" :
n = 10035
print (rearrange(n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int [] storeDigitCounts( int n)
{
int [] arr = new int [10];
for ( int i = 0; i < 10; i++)
arr[i] = 0;
while (n > 0) {
arr[n % 10]++;
n /= 10;
}
return arr;
}
static int rearrange( int n)
{
int [] hash = storeDigitCounts(n);
for ( int mult = 2; mult < 10; mult++) {
int curr = n * mult;
int [] hash_curr = storeDigitCounts(curr);
int ind;
for (ind = 0; ind < 10; ind++) {
if (hash_curr[ind] != hash[ind])
break ;
}
if (ind == 10)
return curr;
}
return -1;
}
public static void Main( string [] args)
{
int n = 10035;
Console.WriteLine(rearrange(n));
}
}
|
Javascript
function storeDigitCounts(n, Hash)
{
while (n > 0)
{
Hash[n % 10] += 1;
n = Math.floor(n / 10);
}
return Hash;
}
function rearrange(n)
{
let hash_n = new Array(10).fill(0);
hash_n = storeDigitCounts(n, hash_n);
for ( var mult = 2; mult < 10; mult++)
{
let curr = n * mult;
let hash_curr = new Array(10).fill(0);
hash_curr = storeDigitCounts(curr, hash_curr) ;
if (JSON.stringify(hash_curr)==JSON.stringify(hash_n))
return curr ;
}
return -1;
}
let n = 10035;
console.log(rearrange(n));
|
Output:
30105
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.