+ 2

# Given 'n' the length of sequence to be formed by all the possible combinations of 0's and 1's.

Also, it should state the number of sequences having even number of 1's in it. write a code in c++.

17 Answers

+ 13

Here we go Amit! (Read the note in comment part)
[https://code.sololearn.com/c9FGgPAf58HW]

+ 14

Increase 8 to 9 or 16 or 32. it depends on your desired maximum support range

+ 14

It seems you want complete solution! :D
So give me a bit time.

+ 13

This is a baseline for your own solution. Good luck, my friend.
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int n = 0;
cout << "Enter n: "; cin >> n;
for (int i = 0; i < n; ++i)
cout << bitset<8>(i).to_string() << endl;
}

+ 11

In our case, in fact, there are different possibilities to attack the problem. Unfortunately,mine wasn't so algorithmic to produce a convincing proof mathematically but rather efficient in terms of speed. (Bitwise stuff covers low-level operations).

+ 3

it is just base. if n > 255 you can split.

+ 3

Besides it was a nice challenge - here we have a python solution:
https://code.sololearn.com/c1unkwlHSyXR
The pointe is: think before code!

+ 3

@ Amit
you have a proof for the formula. I would be interested!

+ 3

@Amit
thanks for that challenge!

+ 2

Okay.βΊ

+ 2

Thank You Babak ππ

+ 2

Actually i have one formula to determine the even number of 1's in all possible combinations of 0's and 1's of length 'n'. that is (2^(n-1) )-1( for this formula n is the power of 2 whereas in the code its different). I verifed the output of the code with this and it matched(Bingo). ex. if n=6, then by that formula i get 31 sequences having even number of 1's. And with 2^6=64 as input in the code i got the same result. Therefore i wanted proof for the formula for higher values of 'n' and it is being fulfilled by the code.

+ 2

Thanks Oma for the solution in Python. ππ

+ 1

Thanks pal #Babak Sheykhan

+ 1

But the code can take max n=255. If n exceeds 255, after 11111111 it starts from 00000000, 00000001 etc again.
And i want a code which shows the number of sequences having even number of 1's.

+ 1

Okay.#Babak.

+ 1

And number of even number of 1's in those sequence??