Print all combinations of points that can compose a given number

You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score n, print out all the combination to compose n.

Examples:

For n = 1, the program should print following:
1

For n = 2, the program should print following:
1 1
2

For n = 3, the program should print following:
1 1 1
1 2
2 1 
3

For n = 4, the program should print following:
1 1 1 1
1 1 2
1 2 1
1 3
2 1 1
2 2
3 1

and so on ...

Algorithm:

  1. At first position we can have three numbers 1 or 2 or 3.
  2. First put 1 at first position and recursively call for n-1.
  3. Then put 2 at first position and recursively call for n-2.
  4. Then put 3 at first position and recursively call for n-3.
  5. If n becomes 0 then we have formed a combination that compose n, so print the current combination.

Below is a generalized implementation. In the below implementation, we can change MAX_POINT if there are higher points (more than 3) in the basketball game.

C/C++

// CPP program to Print all 
// combinations of points that 
// can compose a given number
#define MAX_POINT 3
#define ARR_SIZE 100
#include<stdio.h>

/* Utility function to print array arr[] */
void printArray(int arr[], int arr_size);

/* The function prints all combinations of numbers 1, 2, ...MAX_POINT
that sum up to n.
i is used in recursion keep track of index in arr[] where next
element is to be added. Initital value of i must be passed as 0 */
void printCompositions(int n, int i)
{

    /* array must be static as we want to keep track
    of values stored in arr[] using current calls of
    printCompositions() in function call stack*/
    static int arr[ARR_SIZE];
    
    if (n == 0)
    {
    	printArray(arr, i);
    }
    else if(n > 0)
    {
    	int k; 
    	for (k = 1; k <= MAX_POINT; k++)
    	{
    	arr[i]= k;
    	printCompositions(n-k, i+1);
    	}
    }
}

/* UTILITY FUNCTIONS */
/* Utility function to print array arr[] */
void printArray(int arr[], int arr_size)
{
    int i;
    for (i = 0; i < arr_size; i++)
    	printf("%d ", arr[i]);
    printf("\n");
}

/* Driver function to test above functions */
int main()
{
    int n = 5;
    
    printf("Different compositions formed by 1, 2 and 3 of %d are\n", n);
    printCompositions(n, 0);
    
    getchar();
    return 0;
}

Java

// Java program to Print all 
// combinations of points that 
// can compose a given number
import java.io.*;

class GFG 
{
    // Function prints all combinations of numbers 1, 2, ...MAX_POINT
    // that sum up to n.
    // i is used in recursion keep track of index in arr[] where next
    // element is to be added. Initital value of i must be passed as 0 
    static void printCompositions(int arr[], int n, int i)
    {
        int MAX_POINT = 3;
        if (n == 0)
        {
            printArray(arr, i);
        }
        else if(n > 0)
        {
            for (int k = 1; k <= MAX_POINT; k++)
            {
                arr[i]= k;
                printCompositions(arr, n-k, i+1);
            }
        }
    }
    
    // Utility function to print array arr[] 
    static void printArray(int arr[], int m)
    {
        for (int i = 0; i < m; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
    
    
    // Driver program
    public static void main (String[] args) 
    {
        int n = 5;
        int size = 100;
        int[] arr = new int[size];
        System.out.println("Different compositions formed by 1, 2 and 3 of "+ n + " are");
        printCompositions(arr, n, 0);
    }
}

// Contributed by Pramod Kumar


Output:
Different compositions formed by 1, 2 and 3 of 5 are
1 1 1 1 1 
1 1 1 2 
1 1 2 1 
1 1 3 
1 2 1 1 
1 2 2 
1 3 1 
2 1 1 1 
2 1 2 
2 2 1 
2 3 
3 1 1 
3 2 

Asked by Aloe

Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.