← Home

1015. Smallest Integer Divisible by K

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
101

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1015. Smallest Integer Divisible by K, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • Pigeonhole principle
  • https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/260875/Python-O(K)-with-Detailed-Explanations
  • time: O(K), space: O(K)

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are smallestRepunitDivByK.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(K), space: O(K)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Pigeonhole principle
02//https://leetcode.com/problems/smallest-integer-divisible-by-k/discuss/260875/Python-O(K)-with-Detailed-Explanations
03//Runtime: 104 ms, faster than 7.42% of C++ online submissions for Smallest Integer Divisible by K.
04//Memory Usage: 14.4 MB, less than 5.63% of C++ online submissions for Smallest Integer Divisible by K.
05//time: O(K), space: O(K)
06class Solution {
07public:
08    int smallestRepunitDivByK(int K) {
09        set<int> viableLastDigits= {1,3,7,9};
10        
11        if(viableLastDigits.find(K%10) == viableLastDigits.end()){
12            return -1;
13        }
14        
15        int res = 0;
16        set<int> resSet;
17        
18        /*
19        the residuals of %K are [0,K-1],
20        so we try out 1, 11, 111, 11..111(K '1's)
21        to see if these numbers' residuals fill the holes
22        
23        if there is a duplicate before we find a residual = 0,
24        it will produce a cycle so we will never
25        find a number n s.t. n%K is 0
26        
27        the proof of cycle: n2_res = (10*n1_res + 1) % K,
28        that means the residual of current number is dependent
29        on the previous number
30        */
31        for(int len = 1; len <= K; ++len){
32            res = (res * 10 + 1) % K;
33            if(res == 0) return len;
34            if(resSet.find(res) != resSet.end()) return -1;
35            resSet.insert(res);
36        }
37        
38        return -1;
39    }
40};

Cost

Complexity

Time
O(K), space: O(K)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.