19. Find first set bit
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
Simply iterate over all the bits of the number and check if the bit is set or not.If the bit is set, break loop and return the position of the bit.
Time Complexity : O(log(n))
- The solution has a logarithmic time complexity as it iterates over all the bits of the number.
Auxiliary Space Complexity : O(1)
- The solution uses a constant amount of additional space for variables, regardless of the input size.
To find the position of the first set bit, I'm using bitwise operations.
I'm using the expression 1 + log2(n & ~ (n - 1))
to calculate the position of the first set bit.
Time Complexity : O(1)
- The solution has a constant time complexity as it performs a fixed number of operations, regardless of the input size.
Auxiliary Space Complexity : O(1)
- The solution uses a constant amount of additional space for variables, regardless of the input size.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.