Longest sub string of 0's in a binary string which is repeated K times Last Updated : 28 Oct, 2022 Comments Improve Suggest changes Like Article Like Report Given binary string S of size N and a number K. The task is to find the Longest sub string of 0's in the string which is formed by repeating given string K times. Examples: Input : S = "100001" , K = 3 Output : 4 After repeating given string 3 time, string becomes 100001100001100001. The longest substring of 0's is 4 Input : S = "010001000", K = 4 Output : 4 Approach: If K is one, then find the longest substring of 0's in a string using a simple loopsIf K is greater than one, then add a given string to the end of the string. Then string becomes S+S and length will be 2*N and find the longest substring of 0's in a string using a simple loops If the longest substring is 2*N then, our answer will be K*NOtherwise it will be the our required answer Below is the implementation of the above approach: C++ // C++ program to find the find the Longest continuous // sequence of '0' after repeating Given string K time #include <bits/stdc++.h> using namespace std; // Function to find the longest substring of 0's int longest_substring(string s, int k) { // To store size of the string int n = s.size(); if(k>1) { s += s; n *= 2; } // To store the required answer int ans = 0; // Find the longest substring of 0's int i = 0; while (i < n) { int x = 0; // Run a loop upto s[i] is zero while (s[i] == '0' && i < n) x++, i++; ans = max(ans, x); i++; } // Check the conditions if(k==1 or ans!=n) return ans; else return (ans/2)*k; } // Driver code int main() { string s = "010001000"; int k = 4; // Function call cout << longest_substring(s, k); return 0; } Java // Java program to find the Longest continuous // sequence of '0' after repeating Given string K time class GFG { // Function to find the longest substring of 0's static int longest_substring(String s, int k) { // To store size of the string int n = s.length(); if(k > 1) { s += s; n *= 2; } // To store the required answer int ans = 0; // Find the longest substring of 0's int i = 0; while (i < n) { int x = 0; // Run a loop upto s[i] is zero while (i < n && s.charAt(i) == '0') { x++; i++; } ans = Math.max(ans, x); i++; } // Check the conditions if(k == 1 || ans != n) return ans; else return (ans / 2) * k; } // Driver code public static void main(String[] args) { String s = "010001000"; int k = 4; // Function call System.out.println(longest_substring(s, k)); } } // This code is contributed by Rajput-Ji Python3 # Python3 program to find the find the Longest continuous # sequence of '0' after repeating Given K time # Function to find the longest subof 0's def longest_substring(s, k): # To store size of the string n = len(s) if(k>1): s += s n *= 2 # To store the required answer ans = 0 # Find the longest subof 0's i = 0 while (i < n): x = 0 # Run a loop upto s[i] is zero while (i < n and s[i] == '0'): x,i=x+1, i+1 ans = max(ans, x) i+=1 # Check the conditions if(k==1 or ans!=n): return ans else: return (ans//2)*k # Driver code s = "010001000" k = 4 # Function call print(longest_substring(s, k)) # This code is contributed by mohit kumar 29 C# // C# program to find the Longest continuous // sequence of '0' after repeating Given string K time using System; class GFG { // Function to find the longest substring of 0's static int longest_substring(String s, int k) { // To store size of the string int n = s.Length; if(k > 1) { s += s; n *= 2; } // To store the required answer int ans = 0; // Find the longest substring of 0's int i = 0; while (i < n) { int x = 0; // Run a loop upto s[i] is zero while (i < n && s[i] == '0') { x++; i++; } ans = Math.Max(ans, x); i++; } // Check the conditions if(k == 1 || ans != n) return ans; else return (ans / 2) * k; } // Driver code public static void Main(String[] args) { String s = "010001000"; int k = 4; // Function call Console.WriteLine(longest_substring(s, k)); } } // This code is contributed by PrinciRaj1992 JavaScript <script> // Javascript program to find the find the Longest continuous // sequence of '0' after repeating Given string K time // Function to find the longest substring of 0's function longest_substring(s, k) { // To store size of the string var n = s.length; if(k>1) { s += s; n *= 2; } // To store the required answer var ans = 0; // Find the longest substring of 0's var i = 0; while (i < n) { var x = 0; // Run a loop upto s[i] is zero while (s[i] == '0' && i < n) x++, i++; ans = Math.max(ans, x); i++; } // Check the conditions if(k==1 || ans!=n) return ans; else return (ans/2)*k; } // Driver code var s = "010001000"; var k = 4; // Function call document.write( longest_substring(s, k)); </script> Time Complexity: O(n), where n is the length of the stringAuxiliary Space: O(n), for concatenating the string with itself. Comment More infoAdvertise with us Next Article Longest sub string of 0's in a binary string which is repeated K times M md1844 Follow Improve Article Tags : Strings DSA binary-string LCS substring +1 More Practice Tags : LCSStrings Similar Reads Split the binary string into substrings with equal number of 0s and 1s Given a binary string str of length N, the task is to find the maximum count of consecutive substrings str can be divided into such that all the substrings are balanced i.e. they have equal number of 0s and 1s. If it is not possible to split str satisfying the conditions then print -1.Example: Input 8 min read Find longest sequence of 1's in binary representation with one flip Give an integer n. We can flip exactly one bit. Write code to find the length of the longest sequence of 1 s you could create. Examples: Input : 1775 Output : 8 Binary representation of 1775 is 11011101111.After flipping the highlighted bit, we get consecutive 8 bits. 11011111111.Input : 12 Output : 12 min read Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin 9 min read Replace '?' to convert given string to a binary string with maximum count of '0' and "10" Given string str, consisting of three different types of characters '0', '1' and '?', the task is to convert the given string to a binary string by replacing the '?' characters with either '0' or '1' such that the count of 0s and 10 in the binary string is maximum. Examples: Input: str = 10?0?11Outp 4 min read Minimize count of 0s in Binary String by changing K-size substring to 1s at most Q times Given a binary string S having N characters and two integers K and Q, the task is to find the minimum number of remaining zeroes after at most Q operations, such that in each operation, choose any substring of size at most K and change all its elements to 1. Example: Input: S = 000111, K = 2, Q = 1O 15+ min read Like