Given an integer N. What is the smallest integer greater than N that only has 0 or 1 as its digits?

Yaseen Mollik

I have an integer N. I have to find the smallest integer greater than N that doesn't contain any digit other than 0 or 1. For example: If N = 12 then the answer is 100. I have coded a brute force approach in C++.

int main() {
    long long n;
    cin >> n;

    for (long long i = n + 1; ; i++) {
        long long temp = i;
        bool ok = true;
        while (temp != 0) {
            if ( (temp % 10) != 0 && (temp % 10) != 1) {
                ok = false;
                break;
            }
            temp /= 10;
        }
        if (ok == true) {
            cout << i << endl;
            break;
        }
    }
}

The problem is, my approach is too slow. I believe there is a very efficient approach to solve this. How can I solve this problem efficiently?

Yves Daoust
  1. Increment N,

  2. Starting from the left, scan until you find a digit above 1. Increment the partial number before it and zero out the rest.

E.g.

12 -> 13 -> 1|3 -> 10|0
101 -> 102 -> 10|2 -> 11|0
109 -> 110 -> 110|
111 -> 112 -> 11|2 -> 100|0
198 -> 199 -> 1|99 -> 10|00
1098 -> 1099 -> 10|99 -> 11|00
10203 -> 10204 -> 10|204 -> 11|000
111234 -> 111235 -> 111|235 -> 1000|000
...

Proof:

The requested number must be at least N+1, this is why we increment. We are now looking for a number greater or equal.

Let us call the prefix the initial 0/1 digits and suffix what comes after. We must replace the first digit of the suffix by a zero and set a larger prefix. The smallest prefix that fits is the current prefix plus one. And the smallest suffix that fits is all zeroes.


Update:

I forgot to specify that the prefix must be incremented as a binary number, otherwise forbidden digits could appear.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find smallest integer greater or equal than x (positive integer) multiple of z (positive integer, probably power of 2)

From Dev

why string is greater or less than the integer?

From Dev

smallest number greater than given number which is a palindrome

From Dev

Get 1, 0, -1 as positive, zero, or negative for an integer with math only

From Dev

Find the smallest integer type that can count up to N

From Dev

Java - how to break a integer into its digits

From Dev

if integer is greater than x but less than y (Swift)

From Dev

Integer in String with 1 or 2 digits

From Dev

Select the first N digits of an integer

From Dev

Smallest sum of subarray with sum greater than a given value

From Dev

Check integer value is greater then 0 in Laravel

From Dev

Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced

From Dev

Find the smallest positive integer that does not occur in a given sequence

From Dev

How to find the smallest value greater than 0 in a or-tools 1-D array (python , or-tools )

From Dev

How to find the elements greater than integer

From Dev

I need help making sure that user input is an integer and greater than 0

From Dev

Smallest number with digits product of n

From Dev

Build smallest number larger than K using only allowed digits

From Dev

Find smallest number K , if exists, such that product of its digits is N. Eg:when N = 6, smallest number is k=16(1*6=6) and not k=23(2*3=6)

From Dev

Given a positive integer, n, how can a numerical triangle of height n-1 be printed?

From Dev

Comparing integers and creating the smallest integer possible from the digits of the given integers

From Dev

Using .where condition for results greater than an integer

From Dev

Find the minimum set of integers whose sum is greater than a given integer

From Dev

Thymeleaf switch on integer, case greater than

From Dev

Smallest missing integer algorithm that runs in O(n)?

From Dev

Maximum sum of 1 to n numbers where the sum should never be equal to given integer k

From Dev

How to print all primes which are not greater than the given positive integer N?

From Dev

Why does C outputs gibberish results when operation on an integer greater than 10 digits?

From Dev

Given an integer, w, find n and k such that n^k = w

Related Related

  1. 1

    Find smallest integer greater or equal than x (positive integer) multiple of z (positive integer, probably power of 2)

  2. 2

    why string is greater or less than the integer?

  3. 3

    smallest number greater than given number which is a palindrome

  4. 4

    Get 1, 0, -1 as positive, zero, or negative for an integer with math only

  5. 5

    Find the smallest integer type that can count up to N

  6. 6

    Java - how to break a integer into its digits

  7. 7

    if integer is greater than x but less than y (Swift)

  8. 8

    Integer in String with 1 or 2 digits

  9. 9

    Select the first N digits of an integer

  10. 10

    Smallest sum of subarray with sum greater than a given value

  11. 11

    Check integer value is greater then 0 in Laravel

  12. 12

    Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced

  13. 13

    Find the smallest positive integer that does not occur in a given sequence

  14. 14

    How to find the smallest value greater than 0 in a or-tools 1-D array (python , or-tools )

  15. 15

    How to find the elements greater than integer

  16. 16

    I need help making sure that user input is an integer and greater than 0

  17. 17

    Smallest number with digits product of n

  18. 18

    Build smallest number larger than K using only allowed digits

  19. 19

    Find smallest number K , if exists, such that product of its digits is N. Eg:when N = 6, smallest number is k=16(1*6=6) and not k=23(2*3=6)

  20. 20

    Given a positive integer, n, how can a numerical triangle of height n-1 be printed?

  21. 21

    Comparing integers and creating the smallest integer possible from the digits of the given integers

  22. 22

    Using .where condition for results greater than an integer

  23. 23

    Find the minimum set of integers whose sum is greater than a given integer

  24. 24

    Thymeleaf switch on integer, case greater than

  25. 25

    Smallest missing integer algorithm that runs in O(n)?

  26. 26

    Maximum sum of 1 to n numbers where the sum should never be equal to given integer k

  27. 27

    How to print all primes which are not greater than the given positive integer N?

  28. 28

    Why does C outputs gibberish results when operation on an integer greater than 10 digits?

  29. 29

    Given an integer, w, find n and k such that n^k = w

HotTag

Archive