Find the Shortest All-One Decimal Integer Divisible by K
Quick Overview
Find the Shortest All-One Decimal Integer Divisible by K. Return the length of the smallest positive integer consisting entirely of decimal digit1 that is divisible by k.
Find the Shortest All-One Decimal Integer Divisible by K
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Return the length of the smallest positive integer consisting entirely of decimal digit1 that is divisible by k. Return-1 if none exists. The integer itself may exceed64-bit range; only its length is returned.
```cpp
class Solution {
public:
int smallestRepunitDivByK(int k);
};
```
### Examples
**Example 1:**
```text
Input: k = 7
Output: 6
Explanation: Lengths1 through6 produce remainders1,4,6,5,2,0 modulo7, so111111 is the first divisible repunit.
```
**Example 2:**
```text
Input: k = 5
Output: -1
Explanation: An integer ending in1 cannot be divisible by5.
```
### Constraints
- `1 <= k <= 10^5`.
### Contract considerations
The answer is a digit count, not the repunit value or its residue.
```hint Read the boundary condition
Appending a decimal1 can be simulated entirely modulo k.
```
### Boundary reminders
The answer is a digit count, not the repunit value or its residue.
Overview: Find the Shortest All-One Decimal Integer Divisible by K. Return the length of the smallest positive integer consisting entirely of decimal digit1 that is divisible by k.
Find the Shortest All-One Decimal Integer Divisible by K
Amazon
Jun 20, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Return the length of the smallest positive integer consisting entirely of decimal digit1 that is divisible by k. Return-1 if none exists. The integer itself may exceed64-bit range; only its length is returned.
class Solution {
public:
int smallestRepunitDivByK(int k);
};
Examples
Example 1:
Input: k = 7
Output: 6
Explanation: Lengths1 through6 produce remainders1,4,6,5,2,0 modulo7, so111111 is the first divisible repunit.
Example 2:
Input: k = 5
Output: -1
Explanation: An integer ending in1 cannot be divisible by5.
Constraints
1 <= k <= 10^5
.
Contract considerations
The answer is a digit count, not the repunit value or its residue.
Boundary reminders
The answer is a digit count, not the repunit value or its residue.