Position of leftmost set bit in given binary string where all 1s appear at end Last Updated : 15 Feb, 2022 Comments Improve Suggest changes 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 Position of leftmost set bit in given binary string where all 1s appear at end 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, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha 12 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 Find i'th Index character in a binary string obtained after n iterations Given a decimal number m. Consider its binary representation string and apply n iterations. In each iteration, replace the character 0 with the string 01, and 1 with 10. Find the kth (1-based indexing) character in the string after the nth iterationExamples: Input: m = 5, n = 2, k = 5Output: 0Explan 15+ min read Like