Combinations with repetitions

Suppose we have a string of length- n and we want to generate all combinations/permutations taken r at a time with/without repetitions.  There are four fundamental concepts in Combinatorics

1) Combinations without repetitions/replacements.
2) Combinations with repetitions/replacements.
3) Permutations without repetitions/replacements.
4) Permutations with repetitions/replacements.

Below is a summary table depicting the fundamental concepts in Combinatorics Theory.



Summary Table

Replacements/Repetitions allowed Replacements/Repetitions not allowed
Permutations/Order Important nr possibilities
https://www.geeksforgeeks.org/print-all-combinations-of-given-length/

See the special case when r=n below

https://www.geeksforgeeks.org/print-all-permutations-with-repetition-of-characters

nPr possibilities
https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
Here r=n, as we are permuting all the characters of the string.
Combinations/Order Not Important n+r-1Cr possibilities

Current Article ( https://www.geeksforgeeks.org/combinations-with-repetitions )

nCr possibilities

https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/

This article is about the third case(Order Not important and Repetitions allowed).

The idea is to recur for all the possibilities of the string, even if the characters are repeating.

The base case of the recursion is when there is a total of ‘r’ characters and the combination is ready to be printed.
For clarity, see the recursion tree for the string- “ 1 2 3 4” and r=2

Comb

Below is the implementation.

C


filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to print all combination of size r in an array
// of size n with repetitions allowed
#include <stdio.h>
  
/* arr[]  ---> Input Array
  chosen[] ---> Temporary array to store indices of
                   current combination
   start & end ---> Staring and Ending indexes in arr[]
   r ---> Size of a combination to be printed */
void CombinationRepetitionUtil(int chosen[], int arr[],
                    int index, int r, int start, int end)
{
    // Since index has become r, current combination is
    // ready to be printed, print
    if (index == r)
    {
        for (int i = 0; i < r; i++)
            printf("%d ", arr[chosen[i]]);
        printf("\n");
        return;
    }
  
    // One by one choose all elements (without considering
    // the fact whether element is already chosen or not)
    // and recur
    for (int i = start; i <= end; i++)
    {
        chosen[index] = i;
        CombinationRepetitionUtil(chosen, arr, index + 1,
                                               r, i, end);
    }
    return;
}
  
// The main function that prints all combinations of size r
// in arr[] of size n with repitions. This function mainly
// uses CombinationRepetitionUtil()
void CombinationRepetition(int arr[], int n, int r)
{
    // Allocate memory
    int chosen[r+1];
  
    // Call the recursice function
    CombinationRepetitionUtil(chosen, arr, 0, r, 0, n-1);
}
  
// Driver program to test above functions
int main()
{
    int arr[] = {1, 2, 3, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    int r = 2;
    CombinationRepetition(arr, n, r);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all combination of size r in an array 
// of size n with repetitions allowed 
  
class GFG {
  
    /* arr[] ---> Input Array 
chosen[] ---> Temporary array to store indices of 
                current combination 
start & end ---> Staring and Ending indexes in arr[] 
r ---> Size of a combination to be printed */
    static void CombinationRepetitionUtil(int chosen[], int arr[],
            int index, int r, int start, int end) {
        // Since index has become r, current combination is 
        // ready to be printed, print 
        if (index == r) {
            for (int i = 0; i < r; i++) {
                System.out.printf("%d ", arr[chosen[i]]);
            }
            System.out.printf("\n");
            return;
        }
  
        // One by one choose all elements (without considering 
        // the fact whether element is already chosen or not) 
        // and recur 
        for (int i = start; i <= end; i++) {
            chosen[index] = i;
            CombinationRepetitionUtil(chosen, arr, index + 1,
                    r, i, end);
        }
        return;
    }
  
// The main function that prints all combinations of size r 
// in arr[] of size n with repitions. This function mainly 
// uses CombinationRepetitionUtil() 
    static void CombinationRepetition(int arr[], int n, int r) {
        // Allocate memory 
        int chosen[] = new int[r + 1];
  
        // Call the recursice function 
        CombinationRepetitionUtil(chosen, arr, 0, r, 0, n - 1);
    }
  
// Driver program to test above functions 
    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4};
        int n = arr.length;
        int r = 2;
        CombinationRepetition(arr, n, r);
    }
}
  
/* This Java code is contributed by PrinciRaj1992*/

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all combination of size r in an array 
// of size n with repetitions allowed 
  
using System;
public class GFG{
  
  
    /* arr[] ---> Input Array 
chosen[] ---> Temporary array to store indices of 
                current combination 
start & end ---> Staring and Ending indexes in arr[] 
r ---> Size of a combination to be printed */
    static void CombinationRepetitionUtil(int []chosen, int []arr, 
            int index, int r, int start, int end) { 
        // Since index has become r, current combination is 
        // ready to be printed, print 
        if (index == r) { 
            for (int i = 0; i < r; i++) { 
                Console.Write(arr[chosen[i]]+" "); 
            
            Console.WriteLine(); 
            return
        
  
        // One by one choose all elements (without considering 
        // the fact whether element is already chosen or not) 
        // and recur 
        for (int i = start; i <= end; i++) { 
            chosen[index] = i; 
            CombinationRepetitionUtil(chosen, arr, index + 1, 
                    r, i, end); 
        
        return
    
  
// The main function that prints all combinations of size r 
// in arr[] of size n with repitions. This function mainly 
// uses CombinationRepetitionUtil() 
    static void CombinationRepetition(int []arr, int n, int r) { 
        // Allocate memory 
        int []chosen = new int[r + 1]; 
  
        // Call the recursice function 
        CombinationRepetitionUtil(chosen, arr, 0, r, 0, n - 1); 
    
  
// Driver program to test above functions 
    public static void Main() { 
        int []arr = {1, 2, 3, 4}; 
        int n = arr.Length; 
        int r = 2; 
        CombinationRepetition(arr, n, r); 
    
  
// This code is contributed by PrinciRaj1992 

chevron_right



Output :

1 1 
1 2 
1 3 
1 4 
2 2 
2 3 
2 4 
3 3 
3 4 
4 4

Time Complexity :  For a string of length- n and combinations taken r at a time with repetitions, it takes a total of O(n+r-1Cr) time.

Referenceshttps://en.wikipedia.org/wiki/Combination

This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : princiraj1992



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.