Given a integer n > 0, the task is to find whether in the bit pattern of integer count of continuous 1’s are in increasing from left to right.

**Examples :**

Input:19Output:Yes Explanation: Bit-pattern of 19 = 10011, Counts of continuous 1's from left to right are 1, 2 which are in increasing order.Input :183Output :yes Explanation: Bit-pattern of 183 = 10110111, Counts of continuous 1's from left to right are 1, 2, 3 which are in increasing order.

A **simple solution** is to store binary representation of given number into a string, then traverse from left to right and count the number of continuous 1’s. For every encounter of 0 check the value of previous count of continuous 1’s to that of current value, if the value of previous count is greater than the value of current count then return False, Else when string ends return True.

## C++

`// C++ program to find if bit-pattern ` `// of a number has increasing value of ` `// continuous-1 or not. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns true if n has increasing count of ` `// continuous-1 else false ` `bool` `findContinuous1(` `int` `n) ` `{ ` ` ` `const` `int` `bits = 8*` `sizeof` `(` `int` `); ` ` ` ` ` `// store the bit-pattern of n into ` ` ` `// bit bitset- bp ` ` ` `string bp = bitset <bits>(n).to_string(); ` ` ` ` ` `// set prev_count = 0 and curr_count = 0. ` ` ` `int` `prev_count = 0, curr_count = 0; ` ` ` ` ` `int` `i = 0; ` ` ` `while` `(i < bits) ` ` ` `{ ` ` ` `if` `(bp[i] == ` `'1'` `) ` ` ` `{ ` ` ` `// increment current count of continuous-1 ` ` ` `curr_count++; ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// traverse all continuous-0 ` ` ` `else` `if` `(bp[i-1] == ` `'0'` `) ` ` ` `{ ` ` ` `i++; ` ` ` `curr_count = 0; ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// check prev_count and curr_count ` ` ` `// on encounter of first zero after ` ` ` `// continuous-1s ` ` ` `else` ` ` `{ ` ` ` `if` `(curr_count < prev_count) ` ` ` `return` `0; ` ` ` `i++; ` ` ` `prev_count=curr_count; ` ` ` `curr_count = 0; ` ` ` `} ` ` ` `} ` ` ` ` ` `// check for last sequence of continuous-1 ` ` ` `if` `(prev_count > curr_count && (curr_count != 0)) ` ` ` `return` `0; ` ` ` ` ` `return` `1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 179; ` ` ` `if` `(findContinuous1(n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find if bit-pattern ` `# of a number has increasing value of ` `# continuous-1 or not. ` ` ` `# Returns true if n has increasing count of ` `# continuous-1 else false ` `def` `findContinuous1(n): ` ` ` ` ` `# store the bit-pattern of n into ` ` ` `# bit bitset- bp ` ` ` `bp ` `=` `list` `(` `bin` `(n)) ` ` ` `bits ` `=` `len` `(bp) ` ` ` ` ` `# set prev_count = 0 and curr_count = 0. ` ` ` `prev_count ` `=` `0` ` ` `curr_count ` `=` `0` ` ` ` ` `i ` `=` `0` ` ` `while` `(i < bits): ` ` ` `if` `(bp[i] ` `=` `=` `'1'` `): ` ` ` ` ` `# increment current count of continuous-1 ` ` ` `curr_count ` `+` `=` `1` ` ` `i ` `+` `=` `1` ` ` ` ` `# traverse all continuous-0 ` ` ` `elif` `(bp[i ` `-` `1` `] ` `=` `=` `'0'` `): ` ` ` `i ` `+` `=` `1` ` ` `curr_count ` `=` `0` ` ` `continue` ` ` ` ` `# check prev_count and curr_count ` ` ` `# on encounter of first zero after ` ` ` `# continuous-1s ` ` ` `else` `: ` ` ` `if` `(curr_count < prev_count): ` ` ` `return` `0` ` ` `i ` `+` `=` `1` ` ` `prev_count ` `=` `curr_count ` ` ` `curr_count ` `=` `0` ` ` ` ` `# check for last sequence of continuous-1 ` ` ` `if` `(prev_count > curr_count ` `and` `(curr_count !` `=` `0` `)): ` ` ` `return` `0` ` ` ` ` `return` `1` ` ` `# Driver code ` `n ` `=` `179` `if` `(findContinuous1(n)): ` ` ` `print` `( ` `"Yes"` `) ` `else` `: ` ` ` `print` `( ` `"No"` `) ` ` ` `# This code is contributed by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

**Output :**

Yes

An **efficient solution** is to use decimal to binary conversion loop that divides number by 2 and take remainder as bit. This loop finds bits from right to left. So we check if right to left is in decreasing order or not.

Below is the implementation.

## C++

`// C++ program to check if counts of consecutive ` `// 1s are increasing order. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns true if n has counts of consecutive ` `// 1's are increasing order. ` `bool` `areSetBitsIncreasing(` `int` `n) ` `{ ` ` ` `// Initialize previous count ` ` ` `int` `prev_count = INT_MAX; ` ` ` ` ` `// We traverse bits from right to left ` ` ` `// and check if counts are decreasing ` ` ` `// order. ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `// Ignore 0s until we reach a set bit. ` ` ` `while` `(n > 0 && n % 2 == 0) ` ` ` `n = n/2; ` ` ` ` ` `// Count current set bits ` ` ` `int` `curr_count = 1; ` ` ` `while` `(n > 0 && n % 2 == 1) ` ` ` `{ ` ` ` `n = n/2; ` ` ` `curr_count++; ` ` ` `} ` ` ` ` ` `// Compare current with previous and ` ` ` `// update previous. ` ` ` `if` `(curr_count >= prev_count) ` ` ` `return` `false` `; ` ` ` `prev_count = curr_count; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `if` `(areSetBitsIncreasing(n)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to check if counts of ` `// consecutive 1s are increasing order. ` `import` `java .io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns true if n has counts of ` ` ` `// consecutive 1's are increasing ` ` ` `// order. ` ` ` `static` `boolean` `areSetBitsIncreasing(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Initialize previous count ` ` ` `int` `prev_count = Integer.MAX_VALUE; ` ` ` ` ` `// We traverse bits from right to ` ` ` `// left and check if counts are ` ` ` `// decreasing order. ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` ` ` `// Ignore 0s until we reach ` ` ` `// a set bit. ` ` ` `while` `(n > ` `0` `&& n % ` `2` `== ` `0` `) ` ` ` `n = n/` `2` `; ` ` ` ` ` `// Count current set bits ` ` ` `int` `curr_count = ` `1` `; ` ` ` `while` `(n > ` `0` `&& n % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `n = n/` `2` `; ` ` ` `curr_count++; ` ` ` `} ` ` ` ` ` `// Compare current with previous ` ` ` `// and update previous. ` ` ` `if` `(curr_count >= prev_count) ` ` ` `return` `false` `; ` ` ` `prev_count = curr_count; ` ` ` `} ` ` ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` ` ` `if` `(areSetBitsIncreasing(n)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to check if counts of ` `# consecutive 1s are increasing order. ` ` ` `import` `sys ` ` ` `# Returns true if n has counts of ` `# consecutive 1's are increasing order. ` `def` `areSetBitsIncreasing(n): ` ` ` ` ` `# Initialize previous count ` ` ` `prev_count ` `=` `sys.maxsize ` ` ` ` ` `# We traverse bits from right to ` ` ` `# left and check if counts are ` ` ` `# decreasing order. ` ` ` `while` `(n > ` `0` `): ` ` ` ` ` `# Ignore 0s until we reach a ` ` ` `# set bit. ` ` ` `while` `(n > ` `0` `and` `n ` `%` `2` `=` `=` `0` `): ` ` ` `n ` `=` `int` `(n` `/` `2` `) ` ` ` ` ` `# Count current set bits ` ` ` `curr_count ` `=` `1` ` ` `while` `(n > ` `0` `and` `n ` `%` `2` `=` `=` `1` `): ` ` ` ` ` `n ` `=` `n` `/` `2` ` ` `curr_count ` `+` `=` `1` ` ` ` ` `# Compare current with previous ` ` ` `# and update previous. ` ` ` `if` `(curr_count >` `=` `prev_count): ` ` ` `return` `False` ` ` `prev_count ` `=` `curr_count ` ` ` ` ` `return` `True` ` ` `# Driver code ` `n ` `=` `10` ` ` `if` `(areSetBitsIncreasing(n)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if counts of ` `// consecutive 1s are increasing order. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns true if n has counts of ` ` ` `// consecutive 1's are increasing ` ` ` `// order. ` ` ` `static` `bool` `areSetBitsIncreasing(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Initialize previous count ` ` ` `int` `prev_count = ` `int` `.MaxValue; ` ` ` ` ` `// We traverse bits from right to ` ` ` `// left and check if counts are ` ` ` `// decreasing order. ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` ` ` `// Ignore 0s until we reach ` ` ` `// a set bit. ` ` ` `while` `(n > 0 && n % 2 == 0) ` ` ` `n = n/2; ` ` ` ` ` `// Count current set bits ` ` ` `int` `curr_count = 1; ` ` ` `while` `(n > 0 && n % 2 == 1) ` ` ` `{ ` ` ` `n = n/2; ` ` ` `curr_count++; ` ` ` `} ` ` ` ` ` `// Compare current with previous ` ` ` `// and update previous. ` ` ` `if` `(curr_count >= prev_count) ` ` ` `return` `false` `; ` ` ` `prev_count = curr_count; ` ` ` `} ` ` ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `if` `(areSetBitsIncreasing(n)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check if ` `// counts of consecutive ` `// 1s are increasing order. ` ` ` `// Returns true if n has ` `// counts of consecutive ` `// 1's are increasing order. ` `function` `areSetBitsIncreasing( ` `$n` `) ` `{ ` ` ` `// Initialize previous count ` ` ` `$prev_count` `= PHP_INT_MAX; ` ` ` ` ` `// We traverse bits from right ` ` ` `// to left and check if counts ` ` ` `// are decreasing order. ` ` ` `while` `(` `$n` `> 0) ` ` ` `{ ` ` ` `// Ignore 0s until we ` ` ` `// reach a set bit. ` ` ` `while` `(` `$n` `> 0 && ` `$n` `% 2 == 0) ` ` ` `$n` `= ` `$n` `/ 2; ` ` ` ` ` `// Count current set bits ` ` ` `$curr_count` `= 1; ` ` ` `while` `(` `$n` `> 0 ` `and` `$n` `% 2 == 1) ` ` ` `{ ` ` ` `$n` `= ` `$n` `/ 2; ` ` ` `$curr_count` `++; ` ` ` `} ` ` ` ` ` `// Compare current with previous ` ` ` `// and update previous. ` ` ` `if` `(` `$curr_count` `>= ` `$prev_count` `) ` ` ` `return` `false; ` ` ` `$prev_count` `= ` `$curr_count` `; ` ` ` `} ` ` ` ` ` `return` `true; ` `} ` ` ` `// Driver code ` `$n` `= 10; ` `if` `(areSetBitsIncreasing(` `$n` `)) ` ` ` `echo` `"Yes"` `; ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed by anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

No

This article is contributed by **Shivam Pradhan (anuj_charm)**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Check whether the number has only first and last bits set | Set 2
- Check if a number has same number of set and unset bits
- Check if all bits can be made same by flipping two consecutive bits
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has two adjacent set bits
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Check whether the number has only first and last bits set
- Count pairs in an array such that both elements has equal set bits
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Count number of binary strings without consecutive 1’s : Set 2
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Print numbers having first and last bits as the only set bits
- Toggle bits of a number except first and last bits
- Sort an array according to count of set bits | Set 2
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Check if all bits of a number are set