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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find permutation of n which is divisible by 3 but not divisible by 6
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- 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
- Length of the smallest substring which contains all vowels
- Lexicographically smallest and largest substring of size k
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- K-th lexicographically smallest unique substring of a given string
- Maximum splits in binary string such that each substring is divisible by given odd number
- Lexicographically smallest K-length substring containing maximum number of vowels
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest number divisible by first n numbers
- Smallest N digit number divisible by N
- Smallest K digit number divisible by X
- 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.