forked from chitwang/iitk-sem1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-poem.c
102 lines (86 loc) · 1.81 KB
/
binary-poem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*A Noob-Robot is asking from you to write a program to print a binary poem which consists of only 1's and 0's.
You're given a number N. And, your task is print all string of length 2*N which consists of N 1's and N 0's in lexico-graphical order.
A string S is lexicographically smaller than another string T if S comes before T in the dictionary.
For e.g
if S = "1001" and T = "1010" then S is lexicographically smaller than T.
if S = "10" and T = "01" then T is lexicographically smaller than S.
Input Format
A single line containing an integer N.
Output Format
Print all strings of length 2*N which consist of N 1's and N 0's in lexico-graphical order.
Example Input
3
Example Output
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000*/
// solution:
#include <stdio.h>
#include <stdlib.h>
void print(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d", *(arr + i));
}
printf("\n");
}
void recursion(int n, int *arr, int one, int zero)
{
int x = one + zero;
if (one == n && zero == n)
{
print(arr, x);
return;
}
if (one < n && zero < n)
{
*(arr + x) = 0;
recursion(n, arr, one, zero + 1);
*(arr + x) = 1;
recursion(n, arr, one + 1, zero);
return;
}
if (one < n && zero == n)
{
*(arr + x) = 1;
recursion(n, arr, one + 1, zero);
return;
}
if (zero < n && one == n)
{
*(arr + x) = 0;
recursion(n, arr, one, zero + 1);
return;
}
if (zero > n || one > n)
{
return;
}
}
int main()
{
// Insert your code here.
int n;
scanf("%d", &n);
int *arr = (int *)malloc(2 * n * sizeof(int));
recursion(n, arr, 0, 0);
return 0;
}