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.
- Print all combinations of balanced parentheses
- Prime points (Points that split a number into two primes)
- Print all combinations of factors (Ways to factorize)
- Print all possible combinations of r elements in a given array of size n
- Print combinations of distinct numbers which add up to give sum N
- Number of Integral Points between Two Points
- Find all combinations that add upto given number
- Steps required to visit M points in order on a circular ring of N points
- Number of quadrilaterals possible from the given points
- Number of triangles that can be formed with given N points
- Maximum number of segments that can contain the given points
- Number of triangles formed from a set of points on three lines
- Number of cells in matrix which are equidistant from given two points
- Number of lines from given N points not parallel to X or Y axis
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Number of triangles in a plane if no more than two points are collinear
- Number of Distinct Meeting Points on a Circular Road
- Number of pairs of lines having integer intersection points
- Count distinct points visited on the number line
- Number of horizontal or vertical line segments to connect 3 points