Given a binary string S of length N and an integer K, the task is to find the smallest non-zero sub-string of S that can be jumbled to produce a binary string divisible by 2K. If no such sub-string exists then print -1. Note that K is always greater than 0.
Input: S = “100”, k = 1
Smallest substring that can be jumbled is “10”.
Thus, the answer is 2.
Input: S = “1111”, k = 2
Approach: Let’s look at the condition of the permutation of a string being divisible by 2K.
- The string must have at least K number of 0s.
- The string must have at least one 1.
This can be implemented using two-pointer technique. For every index i, try to find the smallest index j such that the substring S[i…j-1] satisfies the above two conditions.
Let’s say the left pointer is pointing at index i and the right pointer is pointing at j and ans stores the length of the smallest required substring.
If the condition is not satisfied then increment j, else increment i.
While iterating, find the minimum (j – i) satisfying the above two conditions and update the answer as ans = min(ans, j – i).
Below is the implementation of the above approach:
Time Complexity: O(N)
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find smallest permutation of given number
- Check if any permutation of a number is divisible by 3 and is Palindromic
- Check if any permutation of a large number is divisible by 8
- Lexicographically smallest permutation of a string with given subsequences
- Lexicographically smallest permutation with no digits at Original Index
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest and largest substring of size k
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest K digit number divisible by X
- Smallest number divisible by first n numbers
- Length of the smallest number which is divisible by K and formed by using 1's only
- Smallest n digit number divisible by given three numbers
- Smallest number divisible by n and has at-least k trailing zeros
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.