-
Notifications
You must be signed in to change notification settings - Fork 0
/
printAlternatively.cpp
60 lines (59 loc) · 2.23 KB
/
printAlternatively.cpp
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
#include<bits/stdc++.h>
using namespace std;
void printArr(int arr[], int n) {
for (int i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void generate(int A[], int B[], int C[], int i, int j, int m, int n, int len, bool flag){
if (flag) {
// Include valid element from A
// Print output if there is at least one 'B' in output array 'C'
if (len != 0) {
printArr(C, len + 1);
}
// Recur for all elements of A after current index
for (int k = i; k < m; k++) {
if (len == 0) { //this block works for the very first call to include the first element in the output array
C[len] = A[k];
// don't increment len as B is included
generate(A, B, C, k + 1, j, m, n, len, !flag);
}
else if (A[k] > C[len]) { // include valid element from A and recur
C[len + 1] = A[k];
generate(A, B, C, k + 1, j, m, n, len + 1, !flag);
}
}
}
else { //Include valid element from B and recur
for (int l = j; l < n; l++) {
if (B[l] > C[len]) {
C[len + 1] = B[l];
generate(A, B, C, i, l + 1, m, n, len + 1, !flag);
}
}
}
}
int main()
{
int m,n;
cout<<"Enter the length of both arrays : ";
cin>>m;
cin>>n;
int A[m];
int B[n];
cout<<"Enter the elements of first array : ";
for(int i=0;i<m;i++)
{
cin>>A[i];
}
cout<<"Enter the elements of second array : ";
for(int i=0;i<n;i++)
{
cin>>B[i];
}
int C[m+n];
generate(A, B, C, 0, 0, m, n, 0, true);
}