# Find the possible permutation of the bits of N

Given an integer N, the task is to find whether the bits of N can be arranged in alternating manner i.e. either 0101… or 10101…. Assume that N is represented as a 32 bit integer.

Examples:

Input: N = 23
Output: No
“00000000000000000000000000010111” is the binary representation of 23
and the required permutation of bits is not possible.

Input: N = 524280
Output: Yes
binary(524280) = “00000000000001111111111111111000” which can be
rearranged to “01010101010101010101010101010101”.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Since the given integer has to be represented in 32 bits and the number of 1s must be equal to the number of 0s in its binary representation to satisfy the given condition. So, the number of set bits in N must be 16 which can be easily calculated using __builtin_popcount()

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `TOTAL_BITS = 32; ` ` `  `// Function that returns true if it is ` `// possible to arrange the bits of ` `// n in alternate fashion ` `bool` `isPossible(``int` `n) ` `{ ` ` `  `    ``// To store the count of 1s in the ` `    ``// binary representation of n ` `    ``int` `cnt = __builtin_popcount(n); ` ` `  `    ``// If the number set bits and the ` `    ``// number of unset bits is equal ` `    ``if` `(cnt == TOTAL_BITS / 2) ` `        ``return` `true``; ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 524280; ` ` `  `    ``if` `(isPossible(n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` `     `  `class` `GFG ` `{ ` ` `  `static` `int` `TOTAL_BITS = ``32``; ` ` `  `// Function that returns true if it is ` `// possible to arrange the bits of ` `// n in alternate fashion ` `static` `boolean` `isPossible(``int` `n) ` `{ ` ` `  `    ``// To store the count of 1s in the ` `    ``// binary representation of n ` `    ``int` `cnt = Integer.bitCount(n); ` ` `  `    ``// If the number set bits and the ` `    ``// number of unset bits is equal ` `    ``if` `(cnt == TOTAL_BITS / ``2``) ` `        ``return` `true``; ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `static` `public` `void` `main (String []arr) ` `{ ` `    ``int` `n = ``524280``; ` ` `  `    ``if` `(isPossible(n)) ` `        ``System.out.println(``"Yes"``); ` `    ``else` `        ``System.out.println(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 implementation of the approach  ` `TOTAL_BITS ``=` `32``;  ` ` `  `# Function that returns true if it is  ` `# possible to arrange the bits of  ` `# n in alternate fashion  ` `def` `isPossible(n) : ` ` `  `    ``# To store the count of 1s in the  ` `    ``# binary representation of n  ` `    ``cnt ``=` `bin``(n).count(``'1'``);  ` ` `  `    ``# If the number set bits and the  ` `    ``# number of unset bits is equal  ` `    ``if` `(cnt ``=``=` `TOTAL_BITS ``/``/` `2``) : ` `        ``return` `True``;  ` `         `  `    ``return` `False``;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``n ``=` `524280``;  ` ` `  `    ``if` `(isPossible(n)) : ` `        ``print``(``"Yes"``);  ` `    ``else` `: ` `        ``print``(``"No"``);  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the above approach  ` `using` `System;  ` `     `  `class` `GFG ` `{ ` `static` `int` `TOTAL_BITS = 32; ` ` `  `static` `int` `CountBits(``int` `value) ` `{ ` `    ``int` `count = 0; ` `    ``while` `(value != 0) ` `    ``{ ` `        ``count++; ` `        ``value &= value - 1; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Function that returns true if it is ` `// possible to arrange the bits of ` `// n in alternate fashion ` `static` `bool` `isPossible(``int` `n) ` `{ ` ` `  `    ``// To store the count of 1s in the ` `    ``// binary representation of n ` `    ``int` `cnt = CountBits(n); ` ` `  `    ``// If the number set bits and the ` `    ``// number of unset bits is equal ` `    ``if` `(cnt == TOTAL_BITS / 2) ` `        ``return` `true``; ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main (String []arr) ` `{ ` `    ``int` `n = 524280; ` ` `  `    ``if` `(isPossible(n)) ` `        ``Console.WriteLine(``"Yes"``); ` `    ``else` `        ``Console.WriteLine(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by Mohit kumar `

Output:

```Yes
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7101584,"math_prob":0.9426735,"size":4945,"snap":"2019-51-2020-05","text_gpt3_token_len":1348,"char_repetition_ratio":0.15442218,"word_repetition_ratio":0.26622662,"special_character_ratio":0.3007078,"punctuation_ratio":0.093287826,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9993835,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-22T09:25:08Z\",\"WARC-Record-ID\":\"<urn:uuid:12bee0aa-1249-4c03-80cb-c8c2869d818f>\",\"Content-Length\":\"155470\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fadd9179-9fa8-460e-a34c-59737c43fcdc>\",\"WARC-Concurrent-To\":\"<urn:uuid:6c7c3c33-8a54-4822-bb69-1fd4fcebd99d>\",\"WARC-IP-Address\":\"23.219.88.158\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/find-the-possible-permutation-of-the-bits-of-n/\",\"WARC-Payload-Digest\":\"sha1:NAN34FXRBBMIRSCS25KYR4BJL6PORUWK\",\"WARC-Block-Digest\":\"sha1:PAOGCO3MOCQBLW3VWD3QXXZVSA66ABRG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250606872.19_warc_CC-MAIN-20200122071919-20200122100919-00180.warc.gz\"}"}