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.
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 ...
- At first position we can have three numbers 1 or 2 or 3.
- First put 1 at first position and recursively call for n-1.
- Then put 2 at first position and recursively call for n-2.
- Then put 3 at first position and recursively call for n-3.
- 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.
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
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Write a program to print all permutations of a given string
- Minimum number of jumps to reach end
- Print all combinations of balanced parentheses
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Check if a number is multiple of 5 without using / and % operators
- Print all sequences of given length
- Given a number, find the next smallest palindrome
- Count the number of possible triangles
- Print all permutations in sorted (lexicographic) order
- Select a random number from stream, with O(1) space
- Program to convert a given number to words
- Efficient program to print all prime factors of a given number
- Check if a number is Palindrome
- Print all possible combinations of r elements in a given array of size n
- Random number generator in arbitrary probability distribution fashion
- Count digits in given number N which divide N