# Check if all the set bits of the binary representation of N are at least K places away

Given numbers **N** and **K**, The task to check if all the set bits of the binary representation of N are at least K places away.

**Examples:**

Input:N = 5, K = 1Output:YESExplanation:Binary of 5 is 101. The 1's are 1 place far from each other.Input:N = 10, K = 2Output:NOExplanation:Binary of 10 is 1010. The 1's are not at least 2 places far from each other.

**Approach:**

- Iterate Over all the bits in the binary representation of N and maintain a variable ‘count’ initialize to 0.
- Whenever you find a set bit(1), check if count <= K. If not return false.
- If you find a unset bit(0), increases the value of count by 1.

Below is the implementation of the above approach.

`// C++ program to check if all the set ` `// bits of the binary representation ` `// of N are at least K places away. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `CheckBits(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Initialize check and count ` ` ` `// with 0 ` ` ` `int` `check = 0; ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 31; i >= 0; i--) ` ` ` `{ ` ` ` ` ` `// The i-th bit is a set bit ` ` ` `if` `((1 << i) & N) ` ` ` `{ ` ` ` ` ` `// This is the first set bit so, ` ` ` `// start calculating all the ` ` ` `// distances between consecutive ` ` ` `// bits from here ` ` ` `if` `(check == 0) ` ` ` `{ ` ` ` `check = 1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// If count is less than K ` ` ` `// return false ` ` ` `if` `(count < K) ` ` ` `{ ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` `count = 0; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// Adding the count as the ` ` ` `// number of zeroes increase ` ` ` `// between set bits ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 5; ` ` ` `int` `K = 1; ` ` ` ` ` `if` `(CheckBits(N, K)) ` ` ` `{ ` ` ` `cout << ` `"YES"` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `cout << ` `"NO"` `; ` ` ` `} ` ` ` ` ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

YES

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:

- Write an Efficient Method to Check if a Number is Multiple of 3
- Check for Integer Overflow
- Write an Efficient C Program to Reverse Bits of a Number
- Count set bits in an integer
- Count number of bits to be flipped to convert A to B
- Rotate bits of a number
- Next higher number with same number of set bits
- Program to count number of set bits in an (big) array
- Count total set bits in all numbers from 1 to n
- Swap bits in a given number
- Binary representation of a given number
- Swap all odd and even bits
- Check if a number is multiple of 9 using bitwise operators
- Check if binary representation of a number is palindrome
- Toggle all the bits of a number except k-th bit.
- Check if a given number is sparse or not
- 1's and 2's complement of a Binary Number
- Gray to Binary and Binary to Gray conversion
- Cyclic Redundancy Check and Modulo-2 Division
- Check if a number is Bleak

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.