Subsets

Write an algorithm that prints out all the subsets of 3
elements of a set of n elements. The elements of the set are
stored in a list that is the input to the algorithm. (Since
it is a set, you may assume all elements in the list are
distinct.)



Subsets Write an algorithm that prints out all the subsets of 3 elements of a set of n element..

Answer / majid - iran

#include<iostream>
#include<conio.h>
#include<time.h>

using namespace std;

int **m;
long q=0;
long num=0;
long number=0;

long fact( long n )
{
return ( n > 0 ? n*fact(n-1) : 1 );
}

long C( long n , long r )
{
return ( fact(n)/(fact(r)*fact(n-r)) );
}

bool isPrinted(int *a, long n)
{
bool is=true;
for(long i=0;i<num;i++)
{
is=true;
for(long j=0;j<n;j++)
{
if(a[j]!=m[i][j])
{
is=false;
}
}
if(is)return true;
}
return false;
}

void print(int *a,long n)
{
int swap=0;
for(long i=0;i<n;i++)
{
for(long j=i;j<n;j++)
{
if(a[j]<a[i])
{
swap=a[i];
a[i]=a[j];
a[j]=swap;
}
}
}
if(!isPrinted(a,n))
{
number++;
cout<<number<<".{";
for(long i=0;i<n;i++)
{
cout<<a[i];
m[q][i]=a[i];
if(i!=n-1)
{
cout<<",";
}
}
q++;
cout<<"}"<<endl;
}
}

void printAll(int *a,int *b,long n,long k)
{
if(q<num)
{
if(k==1)
{
print(a,n);
}
if(k==0)
{
print(a,n);
}
else if(k>0)
{
int *c, *d;
long p=0;
for(long j=0;j<k;j++)
{
p=0;
c=new int[k-1];
for(long t=0;t<k;t++)
{
if(t!=j)
{
c[p]=b[t];
p++;
}
}
d=new int[n];
for(long i=-1;i<n;i++)
{
for(long t=0;t<n;t++)
{
d[t]=a[t];
}
if(i!=-1)
{
d[i]=b[j];
}
printAll(d,c,n,k-1);
}
}
delete c , d;
}
}
}

int main()
{
time_t start , end;
long n = 0 , k = 0;
cout<<"Enter n:";
cin>>n;
cout<<"Enter k:";
cin>>k;
int *a=new int[n];
for(long i=0;i<n;i++)
{
cin>>a[i];
}
int *b=new int[k];
int *c=new int[n-k];
for(long i=0;i<n;i++)
{
if(i<k)
{
b[i]=a[i];
}
else
{
c[n-i-1]=a[i];
}
}
system("cls");
cout<<"S={";
for( long i = 0 ; i < n ; i++ )
{
cout<<a[i];
if( i != n-1 )
cout<<",";
}
cout<<"}"<<endl;
num=C(n,k);
m=new int*[C(n,k)];
for(long i=0;i<C(n,k);i++)
{
m[i]=new int[k];
}
start=clock();
printAll(b,c,k,n-k);
delete a , b , c;
end=clock();
cout<<"All of subsets printed in "<<float(end-
start)/float(CLK_TCK)<<" seconds."<<endl;
getch();
return 0;
}

Is This Answer Correct ?    9 Yes 2 No

Post New Answer

More C++ Code Interview Questions

3. Program to find the Sum of give series. a. (1)+(1+2)+(1+2+3)+(1+2+3+4)+……………………………….. b. 1/1+1/9+1/25+1/49+……………...

0 Answers  


swap prog

3 Answers   TCS,


write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n

0 Answers   Jomo Kenyatta University,


PROBLEM #8 The cashier at the counter of a Super Store, Mr. Khazaanchi has the following bundles of rupee cash notes with him: Rs. 1, 2, 5, 10, 50, 100, 500, 1000 A customer comes at his counter with various items that he has shopped. Mr. Khazaanchi totals the item prices and tells the customer his total amount payable. The customer gives Mr. Khazanchi some amount of cash. Find the total number of rupee notes of each denomination (i.e. 1, 2, 5, 10, 50, 100, 500, 1000) Mr. Khazaanchi will have to give to the withdrawer ensuring that the total number of rupee notes are minimum.

1 Answers   AR, ARJ,


output for printf("printf");

0 Answers  






write a function that reverse the elements of an array in place.The function must accept only one pointer value and return void.

0 Answers   HCL, SRCASW,


Write a program that print in screen a tree with its height taken from user by entering number of 4 digits and find the odd numbers then calculate the sum of odd numbers so he get the height of tree?

0 Answers  


how to find out the maximum number out of the three inputs.

6 Answers   ABC, Apple, C3I, HP, TCS,


develop a program to calculate and print body mass index for 200 employees

0 Answers   Jomo Kenyatta University,


write a program to sort 'n' elemnts using bubble sort

1 Answers   IBM,


Coin Problem You are given 9 gold coins that look identical. One is counterfeit and weighs a bit greater than the others, but the difference is very small that only a balance scale can tell it from the real one. You have a balance scale that costs 25 USD per weighing. Give an algorithm that finds the counterfeit coin with as little weighting as possible. Of primary importance is that your algorithm is correct; of secondary importance is that your algorithm truly uses the minimum number of weightings possible. HINT: THE BEST ALGORITHM USES ONLY 2 WEIGHINGS!!!

1 Answers   Motorola, Qatar University,


write a function – oriented program that calculates the sum of the squares from 1 to n. thus, if the input is 3, the output is 14

3 Answers  


Categories