What is the output of the following code?
#include<iostream>
using namespace std;
void printArray(int p[], int n)
{
for (int i = 0; i <= n-1; i++)
cout << p[i] << " ";
cout << endl;
}
void func1(int n)
{
int p[n];
int k = 0;
p[k] = n;
while (true)
{
printArray(p, k+1);
int rem_val = 0;
while (k >= 0 && p[k] == 1)
{
rem_val += p[k];
k--;
}
if (k < 0) return;
p[k]--;
rem_val++;
while (rem_val > p[k])
{
p[k+1] = p[k];
rem_val = rem_val - p[k];
k++;
}
p[k+1] = rem_val;
k++;
}
}
int main()
{
int n=3;
func1(n);
return 0;
}
#include<iostream>
using namespace std;
void printArray(int p[], int n)
{
for (int i = 0; i <= n-1; i++)
cout << p[i] << " ";
cout << endl;
}
void func1(int n)
{
int p[n];
int k = 0;
p[k] = n;
while (true)
{
printArray(p, k+1);
int rem_val = 0;
while (k >= 0 && p[k] == 1)
{
rem_val += p[k];
k--;
}
if (k < 0) return;
p[k]--;
rem_val++;
while (rem_val > p[k])
{
p[k+1] = p[k];
rem_val = rem_val - p[k];
k++;
}
p[k+1] = rem_val;
k++;
}
}
int main()
{
int n=3;
func1(n);
return 0;
}A.
3
1 2
1 1 1B.
1 1 1
2 1
3C.
1 1 1
1 2
3D.
3
2 1
1 1 1Answer: Option D
Related Questions on Miscellaneous on Data Structures
Which data structure is used to implement a binary heap efficiently?
A. Array
B. Linked List
C. Stack
D. Queue
In which scenario would you use a Bloom Filter?
A. For implementing a stack-based algorithm
B. To maintain a balanced binary tree
C. For efficient sorting of elements
D. To test membership in a large dataset
A. Queue
B. Stack
C. Heap
D. Array

Join The Discussion