Position of leftmost set bit in given binary string where all 1s appear at end Last Updated : 15 Feb, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a binary string S of length N, such that all 1s appear on the right. The task is to return the index of the first set bit found from the left side else return -1. Examples: Input: s = 00011, N = 5Output: 3Explanation: The first set bit from the left side is at index 3. Input: s = 0000, N = 4Output: -1 Approach: This problem can be solved using Binary Search. Apply Binary search in the range [1, N] to find the first set bit as follows:Update mid as (l+r)/2If s[mid] is set bit, update ans as mid and r as mid-1else update l as mid + 1Return the last stored value in ans. Below is the implementation of the above approach: C++ // C++ Program to find Position // Of leftmost set bit #include <iostream> using namespace std; // Function to find // A bit is set or not bool isSetBit(string& s, int i) { return s[i] == '1'; } // Function to find // First set bit int firstSetBit(string& s, int n) { long l = 0, r = n, m, ans = -1; // Applying binary search while (l <= r) { m = (l + r) / 2; if (isSetBit(s, m)) { // store the current // state of m in ans ans = m; r = m - 1; } else { l = m + 1; } } // Returning the position // of first set bit return ans; } // Driver code int main() { string s = "111"; int n = s.length(); cout << firstSetBit(s, n); return 0; } Java // Java program for the above approach class GFG { // Function to find // A bit is set or not static boolean isSetBit(String s, int i) { return s.charAt(i) == '1' ? true : false; } // Function to find // First set bit static int firstSetBit(String s, int n) { int l = 0, r = n, m, ans = -1; // Applying binary search while (l <= r) { m = (l + r) / 2; if (isSetBit(s, m)) { // store the current // state of m in ans ans = m; r = m - 1; } else { l = m + 1; } } // Returning the position // of first set bit return ans; } // Driver Code public static void main(String args[]) { String s = "111"; int n = s.length(); System.out.print(firstSetBit(s, n)); } } // This code is contributed by gfgking C# // C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to find // A bit is set or not static bool isSetBit(string s, int i) { return s[i] == '1'; } // Function to find // First set bit static int firstSetBit(string s, int n) { int l = 0, r = n, m, ans = -1; // Applying binary search while (l <= r) { m = (l + r) / 2; if (isSetBit(s, m)) { // store the current // state of m in ans ans = m; r = m - 1; } else { l = m + 1; } } // Returning the position // of first set bit return ans; } // Driver Code public static void Main() { string s = "111"; int n = s.Length; Console.Write(firstSetBit(s, n)); } } // This code is contributed by sanjoy_62. JavaScript <script> // JavaScript code for the above approach // Function to find // A bit is set or not function isSetBit(s, i) { return s[i] == '1'; } // Function to find // First set bit function firstSetBit(s, n) { let l = 0, r = n, m, ans = -1; // Applying binary search while (l <= r) { m = Math.floor((l + r) / 2); if (isSetBit(s, m)) { // store the current // state of m in ans ans = m; r = m - 1; } else { l = m + 1; } } // Returning the position // of first set bit return ans; } // Driver code let s = "111"; let n = s.length; document.write(firstSetBit(s, n)); // This code is contributed by Potta Lokesh </script> Python # Python Program to find Position # Of leftmost set bit # Function to find # A bit is set or not def isSetBit(s, i): return s[i] == '1' # Function to find # First set bit def firstSetBit(s, n): l = 0 r = n m = 0 ans = -1 # Applying binary search while (l <= r): m = (l + r) // 2 if (isSetBit(s, m)): # store the current # state of m in ans ans = m r = m - 1 else: l = m + 1 # Returning the position # of first set bit return ans # Driver code s = "111" n = len(s) print(firstSetBit(s, n)) # This code is contributed by Samim Hossain Mondal. Output: 0 Time Complexity: O(LogN)Auxiliary Space: o(1) Comment More infoAdvertise with us Next Article Find k-th bit in a binary string created by repeated invert and append operations C code_r Follow Improve Article Tags : Strings Bit Magic Geeks Premier League DSA Geeks-Premier-League-2022 binary-string +1 More Practice Tags : Bit MagicStrings Similar Reads Maximize subarrays of 0s of length X in given Binary String after flipping at most one '1' Given a binary string str of length N and a positive integer X, the task is to maximize the count of X length subarrays consisting of only 0's by flipping at most one 1. One bit will be considered in only one subarray.Example: Input: str = "0010001", X = 2Output: 3Explanation: Flip str[2] from 1 to 10 min read Find k-th bit in a binary string created by repeated invert and append operations You are given an initial string s starting with "0". The string keeps duplicating as follows. Invert of it is appended to it.Examples: Input : k = 2 Output : 1 Initially s = "0". First Iteration : s = s + s' = "01" Second Iteration : s = s + s' = "0110" The digit at index 2 of s is 1. Input : k = 12 8 min read Count of substrings that start and end with 1 in given Binary String Given a binary string s, the task is to count all substrings that start and end with the character '1'. A valid substring must have both its first and last characters as '1', and can include one or more number of characters in between.Examples:Input: s = "00100101"Output: 3Explanation: Valid substri 7 min read Maximize count of 0s in left and 1s in right substring by splitting given Binary string Given a binary string str, the task is to split the given binary string at any index into two non-empty substrings such that the sum of counts of 0s in the left substring and 1s in the right substring is maximum. Print the sum of such 0s and 1s in the end.Examples: Input: str = "0011110011" Output: 6 min read Maximize sum of assigned weights by flipping at most K bits in given Binary String Given a binary string str of length N and an integer K, the task is to find the maximum possible sum of assigned weights that can be obtained by flipping at most K bits in the given binary string. The weight assigned to the characters of this string are as follows: If a character is '0', then the we 13 min read Maximum bitwise OR one of any two Substrings of given Binary String Given a binary string str, the task is to find the maximum possible OR value of any two substrings of the binary string str. Examples: Input: str = "1110010"Output: "1111110"Explanation: On choosing the substring "1110010" and "11100" we get the OR value as "1111110" which is the maximum value. Inpu 8 min read Like