Given a distance K to cover, the task is to find the minimum steps required to cover the distance if steps can be taken in powers of 2 like 1, 2, 4, 8, 16……..
Input : K = 9 Output : 2 Input : K = 343 Output : 6
The minimum steps required can be calculated by reducing K by the highest power of 2 in each step which can be obtained by counting no. of set bits in the binary representation of a number.
Below is the implementation of the above approach:
Time Complexity :
- Minimize the number of steps required to reach the end of the array
- Maximum steps to transform 0 to X with bitwise AND
- Minimum steps to reach target by a Knight | Set 2
- Minimum steps in which N can be obtained using addition or subtraction at every step
- A variation of Rat in a Maze : multiple steps or jumps allowed
- Find minimum steps required to reach the end of a matrix | Set 2
- Game Theory in Balanced Ternary Numeral System | (Moving 3k steps at a time)
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Minimum number of given powers of 2 required to represent a number
- Exact Cover Problem and Algorithm X | Set 1
- Shortest Superstring Problem | Set 2 (Using Set Cover)
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Represent n as the sum of exactly k powers of two | Set 2
- Powers of 2 to required sum
- Count number of trailing zeros in Binary representation of a number using Bitset
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.