C++ Programming Code Examples C++ > Beginners Lab Assignments Code Examples Program to Generate a Random Subset by Coin Flipping Program to Generate a Random Subset by Coin Flipping - This algorithm prints a subset of the given array using coin flipping method. - The time complexity of this algorithm is O(n). - This algorithm takes the input of 'n' data element and prints a possible subset. - For that, it generates n bit random sequence of 0 or 1 like a coin flipping. - Exit. #include<iostream> #include<stdlib.h> using namespace std; int main() { int j, n; cout<<"\nEnter the number of element array have: "; cin>>n; int arr[n]; cout<<"\n"; // Take the input of the array. for(j = 0; j < n; j++) { cout<<"Enter "<<j+1<<" element: "; cin>>arr[j]; } // Print the subset using . cout<<"\nThe random subset of the given set is: \n\t{ "; for(j = 0; j < n; j++) { // Randomly generates 0 or 1 as coin flipping and print the array element if it is 1. if(rand()%2 == 1) cout<<arr[j]<<" "; } cout<<"}"; return 0; }