You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score of n, print out all the combinations 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:
- In the first position, we can have three numbers 1 or 2 or 3.
- First, put 1 at the first position and recursively call for n-1.
- Then put 2 at the first position and recursively call for n-2.
- Then put 3 at the first position and recursively call for n-3.
- If n becomes 0 then we have formed a combination that composes 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++
#define MAX_POINT 3
#define ARR_SIZE 100
#include <bits/stdc++.h>
using namespace std;
void printArray( int arr[], int arr_size);
void printCompositions( int n, int i)
{
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);
}
}
}
void printArray( int arr[], int arr_size)
{
int i;
for (i = 0; i < arr_size; i++)
cout<<arr[i]<< " " ;
cout<<endl;
}
int main()
{
int n = 5;
cout<< "Different compositions formed by 1, 2 and 3 of " <<n<< " are\n" ;
printCompositions(n, 0);
return 0;
}
|
C
#define MAX_POINT 3
#define ARR_SIZE 100
#include<stdio.h>
void printArray( int arr[], int arr_size);
void printCompositions( int n, int i)
{
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);
}
}
}
void printArray( int arr[], int arr_size)
{
int i;
for (i = 0; i < arr_size; i++)
printf ( "%d " , arr[i]);
printf ( "\n" );
}
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
import java.io.*;
class GFG
{
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 );
}
}
}
static void printArray( int arr[], int m)
{
for ( int i = 0 ; i < m; i++)
System.out.print(arr[i] + " " );
System.out.println();
}
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 );
}
}
|
Python3
MAX_POINT = 3 ;
ARR_SIZE = 100 ;
arr = [ 0 ] * ARR_SIZE;
def printCompositions(n, i):
if (n = = 0 ):
printArray(arr, i);
elif (n > 0 ):
for k in range ( 1 ,MAX_POINT + 1 ):
arr[i] = k;
printCompositions(n - k, i + 1 );
def printArray(arr, arr_size):
for i in range (arr_size):
print (arr[i], end = " " );
print ();
n = 5 ;
print ( "Different compositions formed " +
"by 1, 2 and 3 of" , n, " are" );
printCompositions(n, 0 );
|
C#
using System;
class GFG {
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);
}
}
}
static void printArray( int [] arr, int m)
{
for ( int i = 0; i < m; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
}
public static void Main()
{
int n = 5;
int size = 100;
int [] arr = new int [size];
Console.WriteLine( "Different compositions formed"
+ " by 1, 2 and 3 of " + n + " are" );
printCompositions(arr, n, 0);
}
}
|
PHP
<?php
$MAX_POINT = 3;
$ARR_SIZE = 100;
$arr = array ( $ARR_SIZE );
function printCompositions( $n , $i )
{
global $arr , $MAX_POINT , $ARR_SIZE ;
if ( $n == 0)
{
printArray( $arr , $i );
}
else if ( $n > 0)
{
for ( $k = 1; $k <= $MAX_POINT ; $k ++)
{
$arr [ $i ] = $k ;
printCompositions( $n - $k , $i + 1);
}
}
}
function printArray( $arr , $arr_size )
{
for ( $i = 0; $i < $arr_size ; $i ++)
echo $arr [ $i ]. " " ;
echo "\n" ;
}
$n = 5;
echo "Different compositions formed" .
" by 1, 2 and 3 of " . $n . " are\n" ;
printCompositions( $n , 0);
?>
|
Javascript
<script>
function printCompositions(arr, n, i)
{
let MAX_POINT = 3;
if (n == 0)
{
printArray(arr, i);
}
else if (n > 0)
{
for (let k = 1; k <= MAX_POINT; k++)
{
arr[i] = k;
printCompositions(arr, n - k, i + 1);
}
}
}
function printArray(arr, m)
{
for (let i = 0; i < m; i++)
document.write(arr[i] + " " );
document.write( "<br/>" );
}
let n = 5;
let size = 100;
let arr = new Array(size).fill(0);
document.write( "Different compositions formed " +
"by 1, 2 and 3 of " + n + " are" + "<br/>" );
printCompositions(arr, n, 0);
</script>
|
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
Time Complexity:O(3n)
Auxiliary Space:O(n)
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Jul, 2022
Like Article
Save Article