# Check if a number has bits in alternate pattern | Set 1

Given an integer n > 0, the task is to find whether this integer has an alternate pattern in its bits representation. For example- 5 has an alternate pattern i.e. 101.

Print “Yes” if it has an alternate pattern otherwise “No”. Here alternate pattern can be like 0101 or 1010.

Examples:

Input :15Output :No Explanation: Binary representation of 15 is 1111.Input :10Output :Yes Explanation: Binary representation of 10 is 1010.

A simple approach is to find its binary equivalent and then check its bits.

## C++

`// C++ program to find if a number has alternate` `// bit pattern` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Returns true if n has alternate bit pattern` `// else false` `bool` `findPattern(` `int` `n)` `{` ` ` `// Store last bit` ` ` `int` `prev = n % 2;` ` ` `n = n/2;` ` ` `// Traverse through remaining bits` ` ` `while` `(n > 0)` ` ` `{` ` ` `int` `curr = n % 2;` ` ` `// If current bit is same as previous` ` ` `if` `(curr == prev)` ` ` `return` `false` `;` ` ` `prev = curr;` ` ` `n = n / 2;` ` ` `}` ` ` `return` `true` `;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `n = 10;` ` ` `if` `(findPattern(n))` ` ` `cout << ` `"Yes"` `;` ` ` `else` ` ` `cout << ` `"No"` `;` ` ` `return` `0;` `}` |

## Java

`// Java program to find if a number has alternate` `// bit pattern` `class` `Test` `{` ` ` `// Returns true if n has alternate bit pattern` ` ` `// else false` ` ` `static` `boolean` `findPattern(` `int` `n)` ` ` `{` ` ` `// Store last bit` ` ` `int` `prev = n % ` `2` `;` ` ` `n = n/` `2` `;` ` ` ` ` `// Traverse through remaining bits` ` ` `while` `(n > ` `0` `)` ` ` `{` ` ` `int` `curr = n % ` `2` `;` ` ` ` ` `// If current bit is same as previous` ` ` `if` `(curr == prev)` ` ` `return` `false` `;` ` ` ` ` `prev = curr;` ` ` `n = n / ` `2` `;` ` ` `}` ` ` ` ` `return` `true` `;` ` ` `}` ` ` ` ` `// Driver method` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `n = ` `10` `;` ` ` `System.out.println(findPattern(n) ? ` `"Yes"` `: ` `"No"` `);` ` ` ` ` `}` `}` |

## Python3

`# Python3 program to find if a number` `# has alternate bit pattern` `# Returns true if n has alternate` `# bit pattern else false` `def` `findPattern(n):` ` ` `# Store last bit` ` ` `prev ` `=` `n ` `%` `2` ` ` `n ` `=` `n ` `/` `/` `2` ` ` `# Traverse through remaining bits` ` ` `while` `(n > ` `0` `):` ` ` ` ` `curr ` `=` `n ` `%` `2` ` ` `# If current bit is same as previous` ` ` `if` `(curr ` `=` `=` `prev):` ` ` `return` `False` ` ` `prev ` `=` `curr` ` ` `n ` `=` `n ` `/` `/` `2` ` ` ` ` `return` `True` `# Driver code` `n ` `=` `10` `print` `(` `"Yes"` `) ` `if` `(findPattern(n)) ` `else` `print` `(` `"No"` `)` `# This code is contributed by Anant Agarwal.` |

## C#

`// Program to find if a number` `// has alternate bit pattern` `using` `System;` `class` `Test {` ` ` `// Returns true if n has alternate` ` ` `// bit pattern else returns false` ` ` `static` `bool` `findPattern(` `int` `n)` ` ` `{` ` ` `// Store last bit` ` ` `int` `prev = n % 2;` ` ` `n = n / 2;` ` ` `// Traverse through remaining bits` ` ` `while` `(n > 0) {` ` ` `int` `curr = n % 2;` ` ` `// If current bit is same as previous` ` ` `if` `(curr == prev)` ` ` `return` `false` `;` ` ` `prev = curr;` ` ` `n = n / 2;` ` ` `}` ` ` `return` `true` `;` ` ` `}` ` ` `// Driver method` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `n = 10;` ` ` `Console.WriteLine(findPattern(n) ? ` `"Yes"` `: ` `"No"` `);` ` ` `}` `}` `// This code is contributed by Anant Agarwal.` |

## PHP

`<?php` `// PHP program to find if a` `// number has alternate` `// bit pattern` `// Returns true if n has` `// alternate bit pattern` `// else false` `function` `findPattern(` `$n` `)` `{` ` ` ` ` `// Store last bit` ` ` `$prev` `= ` `$n` `% 2;` ` ` `$n` `= ` `$n` `/ 2;` ` ` `// Traverse through` ` ` `// remaining bits` ` ` `while` `(` `$n` `> 0)` ` ` `{` ` ` `$curr` `= ` `$n` `% 2;` ` ` `// If current bit is` ` ` `// same as previous` ` ` `if` `(` `$curr` `== ` `$prev` `)` ` ` `return` `false;` ` ` `$prev` `= ` `$curr` `;` ` ` `$n` `= ` `floor` `(` `$n` `/ 2);` ` ` `}` ` ` `return` `true;` `}` ` ` `// Driver code` ` ` `$n` `= 10;` ` ` `if` `(findPattern(` `$n` `))` ` ` `echo` `"Yes"` `;` ` ` `else` ` ` `echo` `"No"` `;` ` ` ` ` `return` `0;` `// This code is contributed by nitin mittal.` `?>` |

## Javascript

`<script>` `// Javascript program to find if a number` `// has alternate bit pattern` `// Returns true if n has alternate` `// bit pattern else false` `function` `findPattern(n)` `{` ` ` ` ` `// Store last bit` ` ` `let prev = n % 2;` ` ` `n = Math.floor(n / 2);` ` ` `// Traverse through remaining bits` ` ` `while` `(n > 0)` ` ` `{` ` ` `let curr = n % 2;` ` ` `// If current bit is` ` ` `// same as previous` ` ` `if` `(curr == prev)` ` ` `return` `false` `;` ` ` `prev = curr;` ` ` `n = Math.floor(n / 2);` ` ` `}` ` ` `return` `true` `;` `}` `// Driver code` `let n = 10;` `if` `(findPattern(n))` ` ` `document.write(` `"Yes"` `);` `else` ` ` `document.write(` `"No"` `);` ` ` `// This code is contributed by gfgking` `</script>` |

**Output: **

Yes

**Time Complexity: **O(log_{2}n)

**Auxiliary Space: **O(1)

This article is contributed by **Sahil Chhabra (akku)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.