How to shuffle a number so that it can be maximum multiple of the number 30 ?

897 Views Asked by At

If i have a large number (<=10^5 Digits) how can i tell that if i can shuffle the number so that it become a multiple of 30 . if it is possible then i have to find the maximum multiple . Suppose if i have this number 97980 if i make any combination of this number , all the combinations will be divisible by 30 , but the maximum one is 99870 . How can i shuffle this very large number and know whether it is possible or not ?

2

There are 2 best solutions below

2
On

Fortunately (or unfortunately) shuffling digits does not affect whether a number is divisible by 3.

Casting-out-nines will quickly tell you whether a number is divisible by three. If so, then you need some digit to be zero and to transfer this to the units place, in order to have a result that is divisible by $30$.

Otherwise it is not possible to achieve this divisibility by $30$.

To maximize the multiple of $30$, just sort the digits in descending order from left to right (all big digits in high-order positions).

0
On

The number can be rearranged as a multiple of $30$ if and only if:

  • It contains the digit $0$
  • The sum of its digits is divisible by $3$

In order to obtain the maximum multiple of $30$:

  • Rearrange the digits from largest to smallest
  • Divide the result by $30$