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 = 5
Output: 3
Explanation: The first set bit from the left side is at index 3.Input: s = 0000, N = 4
Output: -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)/2
- If s[mid] is set bit, update ans as mid and r as mid-1
- else update l as mid + 1
- Return 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)