Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MultiplesWithLimit {
- public String minMultiples(int N, int[] forbiddenDigits) {
- boolean []canUse = new boolean[10];
- boolean []was = new boolean[N];
- Arrays.fill(canUse, true);
- for (int t : forbiddenDigits) {
- canUse[t] = false;
- }
- Queue<Integer> qRemainder = new LinkedList();
- Queue<String> qNumber = new LinkedList();
- for (int digit = 1; digit <= 9; ++digit) {
- if (canUse[digit]) {
- qRemainder.add(digit % N);
- qNumber.add(Integer.toString(digit));
- was[digit % N] = true;
- }
- }
- while (!qRemainder.isEmpty()) {
- int rem = qRemainder.poll();
- String prev = qNumber.poll();
- if (rem == 0) {
- if (prev.length() >= 9) {
- String result = prev.substring(0, 3) + "..." + prev.substring(prev.length() - 3) + "(" + prev.length() + " digits)";
- return result;
- }
- else {
- return prev;
- }
- }
- for (int digit = 0; digit <= 9; ++digit) {
- if (canUse[digit]) {
- int newRemainder = (rem * 10 + digit) % N;
- if (was[newRemainder]) {
- continue;
- }
- qRemainder.add(newRemainder);
- qNumber.add(prev + Integer.toString(digit));
- was[newRemainder] = true;
- }
- }
- }
- return "IMPOSSIBLE";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement