The maximum sum may be obtained without dividing N also.
N = 12 breaking N into three parts will give 6, 4, and 3 which gives us the sum = 13. Further breaking 6, 4, and 3 into other parts will give us a sum less than or equal to 6, 4, and 3 respectively. Therefore, the maximum answer will be 13.
The first line of input contains an integer T denoting the number of test cases.
The first and the only line of each test case contains a single integer N.
For each test case, in a separate line, print a single integer which is the maximum sum.
You donβt have to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 3000
Time Limit : 1 sec
The idea is to use recursion in each call. In each call we have two options: either break the number or not. Thus, to explore all the possibilities we will use recursion.
We are solving this problem by solving its subproblems and then combining the solutions of those subproblems. If we analyze carefully, we will see that we are solving the same subproblems multiple times. Thus, this problem exhibits overlapping subproblems. Thus, in this approach, we will eliminate the need for solving the same subproblems again and again. The idea is to store the sum which already occurred in the previous calls. For this, we will use an array to store previous sums.
Let us understand by example.
Let N = 12
As we can see, some subproblems are called multiple times.
The same subproblems are shown by the same color.
Thatβs why we are storing the solved subproblems in a map so that we donβt have to solve them again. Whenever a call is made to the solved subproblem, we will directly return the answer of that subproblem stored in the map.
We are solving this problem by solving its subproblems and then combining the solutions of those subproblems. If we analyze carefully, we will see that we are solving the same subproblems multiple times. Thus, this problem exhibits overlapping subproblems. Thus, in this approach, we will eliminate the need for solving the same subproblems again and again. The idea is to store the sum which already occurred in the previous calls. For this, we will use an array to store previous sums.