- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Smallest Integer Divisible by K in Python

Suppose we have a positive integer K, we need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. We have to find the length of N. If there is no such N, return -1. So if the input is like 3, then the output will be 3. The smallest answer will be N = 111.

To solve this, we will follow these steps −

- if k is even, or k is divisible by 5, then return -1
- set r := 0 and N = 1
- for i in range 1 to K + 1
- r := (r * 10 + 1) mod k
- if r = 0, then return i

Let us see the following implementation to get better understanding −

## Example

class Solution(object): def smallestRepunitDivByK(self, K): if K%2==0 or K%5 ==0: return -1 r = 0 N=1 for i in range(1,K+1): r = (r*10 + 1)%K if r == 0: return i ob = Solution() print(ob.smallestRepunitDivByK(11))

## Input

11

## Output

2

- Related Questions & Answers
- Python Program for Smallest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X?
- C++ Programming for Smallest K digit number divisible by X?
- Java Program for Smallest K digit number divisible by X
- Subarray Sums Divisible by K in C++
- Program to find length of smallest sublist that can be deleted to make sum divisible by k in Python
- Largest K digit number divisible by X in C++
- Smallest number that is divisible by first n numbers in JavaScript
- Count all sub-arrays having sum divisible by k
- Count subarrays whose product is divisible by k in C++
- Find nth number that contains the digit k or divisible by k in C++
- Find smallest element greater than K in Python
- C++ Program for Largest K digit number divisible by X?
- Java Program for Largest K digit number divisible by X
- Smallest possible number divisible by all numbers from 1 to n in JavaScript

Advertisements