Largest sub-string of a binary string divisible by 2 Last Updated : 28 Oct, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given binary string str of length N, the task is to find the longest sub-string divisible by 2. If no such sub-string exists then print -1. Examples: Input: str = "11100011" Output: 111000 Largest sub-string divisible by 2 is "111000".Input: str = "1111" Output: -1 There is no sub-string of the given string which is divisible by 2. Naive approach: A naive approach will be to generate all such sub-strings and check if they are divisible by 2. The time complexity of this approach will be O(N3). Better approach: A straightforward approach will be to remove characters from the end of the string while the last character is 1. The moment a 0 is encountered, the current string will be divisible by 2 as it ends at a 0. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the largest // substring divisible by 2 string largestSubStr(string s) { // While the last character of // the string is '1', pop it while (s.size() and s[s.size() - 1] == '1') s.pop_back(); // If the original string had no '0' if (s.size() == 0) return "-1"; else return s; } // Driver code int main() { string s = "11001"; cout << largestSubStr(s); return 0; } Java // Java implementation of the approach class GFG { // Function to return the largest // substring divisible by 2 static String largestSubStr(String s) { // While the last character of // the string is '1', pop it while (s.length() != 0 && s.charAt(s.length() - 1) == '1') s = s.substring(0, s.length() - 1); // If the original string had no '0' if (s.length() == 0) return "-1"; else return s; } // Driver code public static void main (String[] args) { String s = "11001"; System.out.println(largestSubStr(s)); } } // This code is contributed by AnkitRai01 Python3 # Python3 implementation of the approach # Function to return the largest # substring divisible by 2 def largestSubStr(s) : # While the last character of # the string is '1', pop it while (len(s) and s[len(s) - 1] == '1') : s = s[:len(s) - 1]; # If the original string had no '0' if (len(s) == 0) : return "-1"; else : return s; # Driver code if __name__ == "__main__" : s = "11001"; print(largestSubStr(s)); # This code is contributed by AnkitRai01 C# // C# implementation of the approach using System; class GFG { // Function to return the largest // substring divisible by 2 static string largestSubStr(string s) { // While the last character of // the string is '1', pop it while (s.Length != 0 && s[s.Length - 1] == '1') s = s.Substring(0, s.Length - 1); // If the original string had no '0' if (s.Length == 0) return "-1"; else return s; } // Driver code public static void Main () { string s = "11001"; Console.WriteLine(largestSubStr(s)); } } // This code is contributed by AnkitRai01 JavaScript <script> // Javascript implementation of the approach // Function to return the largest // substring divisible by 2 function largestSubStr(s) { // While the last character of // the string is '1', pop it while (s.length && s[s.length - 1] == '1') s = s.substring(0,s.length-1);; // If the original string had no '0' if (s.length == 0) return "-1"; else return s; } // Driver code var s = "11001"; document.write( largestSubStr(s)); </script> Output: 1100 Time Complexity: O(n), where n is the length of the string sAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article Largest sub-string of a binary string divisible by 2 D DivyanshuShekhar1 Follow Improve Article Tags : DSA binary-string substring Similar Reads Longest sub-sequence of a binary string divisible by 3 Given a binary string S of length N, the task is to find the length of the longest sub-sequence in it which is divisible by 3. Leading zeros in the sub-sequences are allowed.Examples:  Input: S = "1001" Output: 4 The longest sub-sequence divisible by 3 is "1001". 1001 = 9 which is divisible by 3.In 9 min read Number of sub-strings in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of substrings of str which are divisible by 2. Leading zeros in a substring are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only substrings which are divisible by 2. Input: str = "10010" Output: 10 Naive appr 4 min read Number of subsequences in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of subsequences of str which are divisible by 2. Leading zeros in a sub-sequence are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only subsequences which are divisible by 2.Input: str = "10010" Output: 22 Naiv 4 min read Decimal representation of given binary string is divisible by 20 or not The problem is to check whether the decimal representation of the given binary number is divisible by 20 or not. Take care, the number could be very large and may not fit even in long long int. The approach should be such that there are zero or the minimum numbers of multiplication and division oper 14 min read Check divisibility of binary string by 2^k Given a binary string and a number k, the task is to check whether the binary string is evenly divisible by 2k or not. Examples : Input : 11000 k = 2Output : YesExplanation :(11000)2 = (24)1024 is evenly divisible by 2k i.e. 4. Input : 10101 k = 3Output : NoExplanation : (10101)2 = (21)1021 is not e 10 min read Like