SlideShare a Scribd company logo
1 of 3
Download to read offline
// Program to print all combination of size r in an array of size n
#include
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r);
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r)
{
// Current combination is ready to be printed, print it
if (index == r)
{
if (data[0]+data[1]+data[2]+data[3]==24)
{
for (int j=0; j= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
// Driver program to test above functions
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int r = 4;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}
Solution
// Program to print all combination of size r in an array of size n
#include
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r);
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r)
{
// Current combination is ready to be printed, print it
if (index == r)
{
if (data[0]+data[1]+data[2]+data[3]==24)
{
for (int j=0; j= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
// Driver program to test above functions
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int r = 4;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}

More Related Content

Similar to Program to print all combination of size r in an array of size n.pdf

write a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdfwrite a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdfarmcomputers
 
c++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfc++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfdhavalbl38
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfherminaherman
 
The language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdfThe language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdfforwardcom41
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfarorasales234
 
Need this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfNeed this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfarorastores
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxclarkjanyce
 
Write a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdfWrite a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdfarpitcomputronics
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdfarihantelehyb
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfmohammadirfan136964
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfannapurnnatextailes
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfIN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfsiva009113
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfshreeaadithyaacellso
 
Write a program that reads in integer as many as the user enters from.docx
Write a program that reads in integer as many as the user enters from.docxWrite a program that reads in integer as many as the user enters from.docx
Write a program that reads in integer as many as the user enters from.docxlez31palka
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
Given an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdfGiven an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdfNicholasflqStewartl
 
Here is the code- I can't get it to work- I need a function that finds.pdf
Here is the code- I can't get it to work- I need a function that finds.pdfHere is the code- I can't get it to work- I need a function that finds.pdf
Here is the code- I can't get it to work- I need a function that finds.pdfdoshirajesh75
 

Similar to Program to print all combination of size r in an array of size n.pdf (20)

5 array
5 array5 array
5 array
 
write a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdfwrite a program that given a list of 20 integers, sorts them accordi.pdf
write a program that given a list of 20 integers, sorts them accordi.pdf
 
c++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfc++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdf
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
The language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdfThe language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdf
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
 
Need this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfNeed this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdf
 
There are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docxThere are a number of errors in the following program- All errors are.docx
There are a number of errors in the following program- All errors are.docx
 
Write a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdfWrite a program in C that generates a list of random numbers greater.pdf
Write a program in C that generates a list of random numbers greater.pdf
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfIN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Write a program that reads in integer as many as the user enters from.docx
Write a program that reads in integer as many as the user enters from.docxWrite a program that reads in integer as many as the user enters from.docx
Write a program that reads in integer as many as the user enters from.docx
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Given an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdfGiven an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdf
 
Arrays
ArraysArrays
Arrays
 
Here is the code- I can't get it to work- I need a function that finds.pdf
Here is the code- I can't get it to work- I need a function that finds.pdfHere is the code- I can't get it to work- I need a function that finds.pdf
Here is the code- I can't get it to work- I need a function that finds.pdf
 

More from info518726

HeadPhone.java All the outputs were same because the variabl.pdf
 HeadPhone.java  All the outputs were same because the variabl.pdf HeadPhone.java  All the outputs were same because the variabl.pdf
HeadPhone.java All the outputs were same because the variabl.pdfinfo518726
 
Methanol is soluble in water. methanol is a one carbon organic alco.pdf
  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf
Methanol is soluble in water. methanol is a one carbon organic alco.pdfinfo518726
 
Solubility in water happens when something is pol.pdf
                     Solubility in water happens when something is pol.pdf                     Solubility in water happens when something is pol.pdf
Solubility in water happens when something is pol.pdfinfo518726
 
moles = 0.144 .pdf
                     moles = 0.144                                    .pdf                     moles = 0.144                                    .pdf
moles = 0.144 .pdfinfo518726
 
It is used to remove water from the organic layer.pdf
                     It is used to remove water from the organic layer.pdf                     It is used to remove water from the organic layer.pdf
It is used to remove water from the organic layer.pdfinfo518726
 
nitrogen is more electronegative than phosphorus .pdf
                     nitrogen is more electronegative than phosphorus .pdf                     nitrogen is more electronegative than phosphorus .pdf
nitrogen is more electronegative than phosphorus .pdfinfo518726
 
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdfinfo518726
 
hydration of alkene. .pdf
                     hydration of alkene.                             .pdf                     hydration of alkene.                             .pdf
hydration of alkene. .pdfinfo518726
 
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdf
                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdfinfo518726
 
he coordination compounds , metal complexes or si.pdf
                     he coordination compounds , metal complexes or si.pdf                     he coordination compounds , metal complexes or si.pdf
he coordination compounds , metal complexes or si.pdfinfo518726
 
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdf
                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdfinfo518726
 
melting point of the mixture decreases when small.pdf
                     melting point of the mixture decreases when small.pdf                     melting point of the mixture decreases when small.pdf
melting point of the mixture decreases when small.pdfinfo518726
 
  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdf  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdfinfo518726
 
What is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdfWhat is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdfinfo518726
 
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdfthe name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdfinfo518726
 
Sucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdfSucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdfinfo518726
 
C. Increasing the temperature of the system .pdf
                     C. Increasing the temperature of the system      .pdf                     C. Increasing the temperature of the system      .pdf
C. Increasing the temperature of the system .pdfinfo518726
 
because Electron Affinity is the amount of energy.pdf
                     because Electron Affinity is the amount of energy.pdf                     because Electron Affinity is the amount of energy.pdf
because Electron Affinity is the amount of energy.pdfinfo518726
 
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdfQues-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdfinfo518726
 
Programprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdfProgramprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdfinfo518726
 

More from info518726 (20)

HeadPhone.java All the outputs were same because the variabl.pdf
 HeadPhone.java  All the outputs were same because the variabl.pdf HeadPhone.java  All the outputs were same because the variabl.pdf
HeadPhone.java All the outputs were same because the variabl.pdf
 
Methanol is soluble in water. methanol is a one carbon organic alco.pdf
  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf
Methanol is soluble in water. methanol is a one carbon organic alco.pdf
 
Solubility in water happens when something is pol.pdf
                     Solubility in water happens when something is pol.pdf                     Solubility in water happens when something is pol.pdf
Solubility in water happens when something is pol.pdf
 
moles = 0.144 .pdf
                     moles = 0.144                                    .pdf                     moles = 0.144                                    .pdf
moles = 0.144 .pdf
 
It is used to remove water from the organic layer.pdf
                     It is used to remove water from the organic layer.pdf                     It is used to remove water from the organic layer.pdf
It is used to remove water from the organic layer.pdf
 
nitrogen is more electronegative than phosphorus .pdf
                     nitrogen is more electronegative than phosphorus .pdf                     nitrogen is more electronegative than phosphorus .pdf
nitrogen is more electronegative than phosphorus .pdf
 
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
 
hydration of alkene. .pdf
                     hydration of alkene.                             .pdf                     hydration of alkene.                             .pdf
hydration of alkene. .pdf
 
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdf
                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdf
 
he coordination compounds , metal complexes or si.pdf
                     he coordination compounds , metal complexes or si.pdf                     he coordination compounds , metal complexes or si.pdf
he coordination compounds , metal complexes or si.pdf
 
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdf
                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdf
 
melting point of the mixture decreases when small.pdf
                     melting point of the mixture decreases when small.pdf                     melting point of the mixture decreases when small.pdf
melting point of the mixture decreases when small.pdf
 
  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdf  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdf
 
What is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdfWhat is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdf
 
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdfthe name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
 
Sucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdfSucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdf
 
C. Increasing the temperature of the system .pdf
                     C. Increasing the temperature of the system      .pdf                     C. Increasing the temperature of the system      .pdf
C. Increasing the temperature of the system .pdf
 
because Electron Affinity is the amount of energy.pdf
                     because Electron Affinity is the amount of energy.pdf                     because Electron Affinity is the amount of energy.pdf
because Electron Affinity is the amount of energy.pdf
 
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdfQues-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
 
Programprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdfProgramprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdf
 

Recently uploaded

e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 

Recently uploaded (20)

e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 

Program to print all combination of size r in an array of size n.pdf

  • 1. // Program to print all combination of size r in an array of size n #include void combinationUtil(int arr[], int data[], int start, int end, int index, int r); // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[r]; // Print all combination using temprary array 'data[]' combinationUtil(arr, data, 0, n-1, 0, r); } /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Staring and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ void combinationUtil(int arr[], int data[], int start, int end, int index, int r) { // Current combination is ready to be printed, print it if (index == r) { if (data[0]+data[1]+data[2]+data[3]==24) { for (int j=0; j= r-index" makes sure that including one element // at index will make a combination with remaining elements // at remaining positions for (int i=start; i<=end && end-i+1 >= r-index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i+1, end, index+1, r); } }
  • 2. // Driver program to test above functions int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int r = 4; int n = sizeof(arr)/sizeof(arr[0]); printCombination(arr, n, r); } Solution // Program to print all combination of size r in an array of size n #include void combinationUtil(int arr[], int data[], int start, int end, int index, int r); // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[r]; // Print all combination using temprary array 'data[]' combinationUtil(arr, data, 0, n-1, 0, r); } /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Staring and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ void combinationUtil(int arr[], int data[], int start, int end, int index, int r) { // Current combination is ready to be printed, print it if (index == r) { if (data[0]+data[1]+data[2]+data[3]==24)
  • 3. { for (int j=0; j= r-index" makes sure that including one element // at index will make a combination with remaining elements // at remaining positions for (int i=start; i<=end && end-i+1 >= r-index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i+1, end, index+1, r); } } // Driver program to test above functions int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int r = 4; int n = sizeof(arr)/sizeof(arr[0]); printCombination(arr, n, r); }