
Let us suppose the passing marks is: 10
So, the number of ways Ninja can score 10 marks are (2, 2, 2, 2, 2) and (5, 5), so you need to print 2.
Keep in mind that a particular test has infinite questions of each type.
Keep in mind that the ninja does not want more than the passing marks.
Please note that (2, 5) and (5, 2) are the same and you need to take them as a single way.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single integer ‘N’ denoting the passing marks of the test.
For each test case, print a single integer that denotes the number of ways to reach the given marks.
Output for every test case will be printed in a separate line.
1 <= T <= 50
0 <= N <= 10000
Time limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the given function.
The simplest idea to solve this problem is to use recursion. For every question, Ninja has two choices, either he will attempt it at least once or not. So we will make a recursive function and count all the ways to get given marks by attempting either 2, 5, or 7 mark questions.
In the recursive approach, some subproblems are overlapped making them to be solved more than one time. This problem can be solved by using memoization.
The recursive approach can be optimized by maintaining an extra 2-d array “res” that stores the result of the recursive calls. For every sub-problem, we first check the “res” array. If the value is already computed we simply return it otherwise we calculate it using recursion and put it in the “res” array.
Algorithm:
The basic approach used to solve this problem is to use a DP array of size ‘N + 1’, that will store the count of all marks from 0 to N, and for every possible question solved we will simply increment the values in the created array.