Skip to content

Commit 059c95c

Browse files
authored
Merge pull request kothariji#108 from gothira/master
Binary code problem.
2 parents 5b0283a + 1119436 commit 059c95c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Binary code problem (Hackerrank)

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*Binary Code
2+
3+
You are given a sequence of bits (a binary string) of length N. You need to generate a list of binary strings of length N starting from the given binary string. There can be no repetitions in the list, and any two consecutive binary strings in the list should differ by only one bit.
4+
5+
For example, assume you are given the binary string 0010. binary strings 0010 and 0110 differ only by one bit; 0010 and 0111 differ by two bits; 0010 and 0010 are the same; 0200 is not a binary string; 100 is not a binary string of length 4, while 0100 is a binary string of length 4.
6+
7+
This problem has partial scores. If your list is valid, then your score for that test case is {L\over 2^N}S, where L is the number of binary strings in your list and S is the allocated score for the test case.
8+
9+
Input Format
10+
The input will contain a non empty string of 0’s and 1’s indicating the initial binary string.
11+
12+
Constraints
13+
1 ≤ N ≤ 16
14+
Limits
15+
Time Limit: 1s
16+
Memory Limit: 256MB
17+
Output Format
18+
First line of the output should contain a single integer L, the number of strings in your list. L lines should follow, the list of values.
19+
20+
Sample Input
21+
0010
22+
23+
Sample Output 0
24+
6
25+
0010
26+
1010
27+
1000
28+
1100
29+
0100
30+
0000
31+
*/
32+
33+
//Solution.
34+
35+
#include <iostream>
36+
#include <algorithm>
37+
#include <vector>
38+
#include <string>
39+
#include <sstream>
40+
#include <map>
41+
#include <set>
42+
#include <cmath>
43+
#include <cstring>
44+
#include <cstdio>
45+
46+
using namespace std;
47+
48+
string s;
49+
50+
void change(int n) {
51+
if (n >= s.size()) {
52+
cout << s << endl;
53+
return;
54+
}
55+
56+
change(n + 1);
57+
s[n] = s[n] == '0' ? '1' : '0';
58+
change(n + 1);
59+
}
60+
61+
int main() {
62+
cin >> s;
63+
64+
cout << (1 << s.size()) << endl;
65+
66+
change(0);
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)