Загрузка...

GFG POTD: Position of the Set Bit | Bit-Magic (Java) | Day 04

Welcome to today's explanation of the GFG POTD (Problem of the Day)!
In this video, I break down the strategies, ideas, and core concepts you need to solve the "Position of the Set Bit" problem. Understanding these underlying data structures will boost your overall problem-solving skills and help you build the intuition to tackle similar questions on your own next time.

If you found this video helpful, please show your gratitude by liking the video, and don't forget to subscribe to the channel for more daily coding content. Happy coding!
Problem Name: Position of the Set Bit
Difficulty: Easy
Platform: GeeksforGeeks

Question: Given a number N having only 1 set bit in its binary representation, find the position of the only set bit. If there are 0 or more than 1 set bits, return -1. Position should be counted starting with 1 from the LSB (least significant bit) side.

The Approach: Right Shifting

To find the position of the only set bit, we employ a bit manipulation technique.
1. Check for Edge Cases: We must first ensure that the input number N has exactly one set bit. A quick way to verify if N is a power of two (and thus contains only one set bit) is checking If not, we immediately return -1.

2. Iterative Right Shift: If N is a power of two, we use a loop. Inside the loop, we initialize a counter (pos) to 1.

3. Find the Bit: We iteratively right-shift N one bit at a time . With each shift, we check if the current number is 1. If it is not 1, we increment the counter (pos++).

4. Identify the Result: The loop continues as long as N is greater than 1. When the loop terminates, the final value of the counter is the 1-based position of the set bit from the LSB side.

Complexity :

Time Complexity: O(log N), where N is the value of the input number. In the worst case, we must iterate through all the bits of the integer, which is proportional to the logarithm of the number to the base 2.

Space Complexity: O(1), as we use only a few integer variables.

#gfg #dsa #starters #coding #problemoftheday #java #bitmanipulation #bitwiseoperators #logNcomplexity #easyquestion #leetcode #competitiveprogramming #hike #flipkart #microsoft #adobeillustrator
Solution : https://github.com/Arnab-Pachal1234/GFG-POTD-SOLUTION
explanation : https://docs.google.com/document/d/1Zh1CMmsuq1-bjGrkZXWjcCGna-YcYcSmzJfElu_KryY/edit?usp=sharing

Видео GFG POTD: Position of the Set Bit | Bit-Magic (Java) | Day 04 канала ARNAB PACHAL
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять