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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Find permutation of n which is divisible by 3 but not divisible by 6
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Check if any permutation of N equals any power of K
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if any permutation of a large number is divisible by 8
- Check if any permutation of a number is divisible by 3 and is Palindromic
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Smallest number divisible by n and has at-least k trailing zeros
- Smallest integer which has n factors or more
- Length of the smallest substring which contains all vowels
- Length of the smallest number which is divisible by K and formed by using 1's only
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Minimum number of times A has to be repeated such that B is a substring of it
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find if a given string can be represented from a substring by iterating the substring “n” times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.