
Input: 'N' = 5, 'DIAMOND' = [1, 2, 3, 4, 5]
Output: 9
Mario can take the following combinations of DIAMOND without getting burn:-
1, 3, 5 = 1 + 3 + 5 = 9.
1, 4 = 1 + 4 = 5
1, 5 = 1+5 = 6
2, 4 = 2+4 = 6
2, 5 = 2+5 = 7
3, 5 = 3+5 = 8
Also, he can take all the DIAMOND uniquely as well means he takes DIAMOND from the single dragon but the max DIAMOND he can get is 9.
The first line will contain the integer 'T', denoting the number of test cases.
For each test case, the first line will contain a single integer 'N', the size of the array 'DIAMOND'
The second line of each test case will contain ‘N’ integers representing the array elements.
For each test case, print the maximum DIAMOND Mario can get.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= DIAMOND[i] <= 10^5
Time Limit: 1 sec
There are ‘N’ dragons on the road and for each dragon, we have two choices either we take the diamond from him or we don’t take the diamond from him. We can not take DIAMOND from two successive dragons so have to make sure of that as well.
Initially, we have 0 DIAMOND and for each ‘i’ from 0 to ‘N’-1 (0-based indexing), we can take DIAMOND from the current dragon if we haven’t taken DIAMOND from the direct previous dragon of the current dragon if there are any, and for that, we will make a flag which we will make false when we take the DIAMOND and we take diamond if the flag is true and if we don’t take the diamond than make the flag true and call for the next index and return the max of the cases where we took the DIAMOND from the current dragon and we didn’t take the diamond from the current dragons.
The Steps are as follows:-
If there are ‘N’ dragons and we want to take the maximum number of DIAMOND then if we closely look, We can take the maximum number of DIAMOND from ‘N’ dragons by taking a maximum of the following 2:-
Now the base case will be when there is 0 dragon then we can not take any diamond and where there is only 1 dragon then we can take the DIAMOND from that dragon only and then we can use the above approach for all the dragons till ‘N’. The only problem here is we are calculating the same thing over and over again so we can store the answer in a DP array when it is first called then on another call we will directly return it from the DP array.
If there are ‘N’ dragons and we want to take the maximum number of DIAMOND then if we closely look, We can take the maximum number of DIAMOND from ‘N’ dragons by taking a maximum of the following:-