# Count number of steps to cover a distance if steps can be taken in powers of 2

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……..

**Examples :**

Input :K = 9Output :2Input :K = 343Output :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:

## C++

`// C++ program to count the minimum number of steps ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the minimum number of steps ` `int` `getMinSteps(` `int` `K) ` `{ ` ` ` `// __builtin_popcount() is a C++ function to ` ` ` `// count the number of set bits in a number ` ` ` `return` `__builtin_popcount(k); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 343; ` ` ` ` ` `cout << getMinSteps(n)<< ` `'\n'` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count minimum number of steps ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to count the minimum number of steps ` ` ` `static` `int` `getMinSteps(` `int` `K) ` ` ` `{ ` ` ` `// count the number of set bits in a number ` ` ` `return` `Integer.bitCount(K); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `343` `; ` ` ` ` ` `System.out.println(getMinSteps(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to count the minimum number of steps ` `def` `getMinSteps(K) : ` ` ` ` ` `# bin(K).count("1") is a Python3 function to ` ` ` `# count the number of set bits in a number ` ` ` `return` `bin` `(K).count(` `"1"` `) ` ` ` `# Driver Code ` `n ` `=` `343` `print` `(getMinSteps(n)) ` ` ` `# This code is contributed by ` `# divyamohan123 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count minimum number of steps ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to count the minimum number of steps ` ` ` `static` `int` `getMinSteps(` `int` `K) ` ` ` `{ ` ` ` `// count the number of set bits in a number ` ` ` `return` `countSetBits(K); ` ` ` `} ` ` ` ` ` `static` `int` `countSetBits(` `int` `x) ` ` ` `{ ` ` ` `int` `setBits = 0; ` ` ` `while` `(x != 0) ` ` ` `{ ` ` ` `x = x & (x - 1); ` ` ` `setBits++; ` ` ` `} ` ` ` `return` `setBits; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `n = 343; ` ` ` ` ` `Console.WriteLine(getMinSteps(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity : **

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimize the number of steps required to reach the end of the array
- Find minimum number of steps to reach the end of String
- Minimum number of steps to make all elements of array equal
- Number of steps to sort the array by changing order of three elements in each step
- Maximum steps to transform 0 to X with bitwise AND
- Minimum steps to reach target by a Knight | Set 2
- A variation of Rat in a Maze : multiple steps or jumps allowed
- Minimum steps in which N can be obtained using addition or subtraction at every step
- Find minimum steps required to reach the end of a matrix | Set 2
- Minimum steps to reach the Nth stair in jumps of perfect power of 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
- Finding powers of any number P in N!
- Find all powers of 2 less than or equal to a given number
- Shortest Superstring Problem | Set 2 (Using Set Cover)
- Exact Cover Problem and Algorithm X | Set 1
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Powers of 2 to required sum
- Represent n as the sum of exactly k powers of two | Set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.