+ 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++.

7th Oct 2017, 6:33 AM
Amit Gharge
Amit Gharge - avatar
17 Answers
+ 13
Here we go Amit! (Read the note in comment part) [https://code.sololearn.com/c9FGgPAf58HW]
7th Oct 2017, 8:36 AM
Babak
Babak - avatar
+ 14
Increase 8 to 9 or 16 or 32. it depends on your desired maximum support range
7th Oct 2017, 7:49 AM
Babak
Babak - avatar
+ 14
It seems you want complete solution! :D So give me a bit time.
7th Oct 2017, 7:53 AM
Babak
Babak - avatar
+ 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; }
7th Oct 2017, 7:11 AM
Babak
Babak - avatar
+ 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).
7th Oct 2017, 10:41 AM
Babak
Babak - avatar
+ 3
it is just base. if n > 255 you can split.
7th Oct 2017, 7:45 AM
Oma Falk
Oma Falk - avatar
+ 3
Besides it was a nice challenge - here we have a python solution: https://code.sololearn.com/c1unkwlHSyXR The pointe is: think before code!
7th Oct 2017, 12:11 PM
Oma Falk
Oma Falk - avatar
+ 3
@ Amit you have a proof for the formula. I would be interested!
7th Oct 2017, 12:12 PM
Oma Falk
Oma Falk - avatar
+ 3
@Amit thanks for that challenge!
7th Oct 2017, 3:54 PM
Oma Falk
Oma Falk - avatar
+ 2
Okay.☺
7th Oct 2017, 7:54 AM
Amit Gharge
Amit Gharge - avatar
+ 2
Thank You Babak 😊😊
7th Oct 2017, 8:41 AM
Amit Gharge
Amit Gharge - avatar
+ 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.
7th Oct 2017, 8:50 AM
Amit Gharge
Amit Gharge - avatar
+ 2
Thanks Oma for the solution in Python. 😊😊
7th Oct 2017, 12:16 PM
Amit Gharge
Amit Gharge - avatar
+ 1
Thanks pal #Babak Sheykhan
7th Oct 2017, 7:22 AM
Amit Gharge
Amit Gharge - avatar
+ 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.
7th Oct 2017, 7:23 AM
Amit Gharge
Amit Gharge - avatar
+ 1
Okay.#Babak.
7th Oct 2017, 7:50 AM
Amit Gharge
Amit Gharge - avatar
+ 1
And number of even number of 1's in those sequence??
7th Oct 2017, 7:52 AM
Amit Gharge
Amit Gharge - avatar