Click here to Skip to main content
15,886,634 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
While attending Geeks Classes, Shivani went through a question in which an array was given and she was asked to maximize the sum by picking some elements from the array so that after selecting an element A[i] from array, delete one occurrence of A[i]-1 (If array size becomes 1, you can include that single element in sum). But, she got stuck and asked you for help. Help Shivani in solving this question.
Note : Elements to be selected are from maximum to minimum.

Input :
The first line of the input contains T denoting the number of the test cases. For each test case T, the first line contains an integer N denoting the size of the array. Next line contains N space separated integers denoting the elements of the array.

Output :
For each test case, the output is an integer displaying the maximum sum of selected numbers.

Constraints :
1 <= T <= 100
1 <= n <= 50
1 <= A[i] <= 20

Example :
Input
2
3
1 2 3
6
1 2 2 2 3 4
Output
4
10

What I have tried:

Please Help Me Out..!!
Just Give me the explanation.
Posted
Updated 15-Apr-21 11:52am

We see this sort of thing so often that @originalgriff wrote an article about how to start ... start here How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
OriginalGriff 23-Mar-21 4:02am    
Snap! :laugh:
+5
CHill60 23-Mar-21 4:05am    
I went for the terse version :laugh:
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
And part of the homework is getting you to work out how to analyze a problem and come up with solutions - so "explaining it" to you really wouldn't help you in the long term!

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^] and think about how you would do it manually, given a set of numbers on a sheet of paper.
 
Share this answer
 
v2
Comments
Ananth V 23-Mar-21 4:08am    
I'm not asking you the code. I just need a better explanation. This is the only reason, why I uploaded the question here.
OriginalGriff 23-Mar-21 4:47am    
A better explanation of what?
The homework assignment is pretty clear - try it on paper and you'll see what it means. If you still can't understand it, you need to talk to your teacher and get him to clarify things for you.
Quote:
While attending Geeks Classes, Shivan ...

First of all, this comes from a challenge site, it is a good idea to give link to original page, because in those problems, exact wording matters.
Quote:
How can I solve this coding problem.

The first step is to solve the problem by hand with a pencil and sheet of paper.
When you are familiar with the procedure you use to solve, it is basically your algorithm,you should be able to start write code.
Quote:
Just Give me the explanation.

Have a look at Maximize the sum of selected numbers from an array to make it empty | Practice | GeeksforGeeks[^]
 
Share this answer
 
Comments
Richard MacCutchan 23-Mar-21 7:38am    
I must be very stupid, as the explanation does not make sense to me.
Patrice T 23-Mar-21 9:16am    
My understanding is
until array is empty : Take one maximum N and remove one N-1 if any.
Richard MacCutchan 23-Mar-21 9:33am    
I understand that part. What I don't understand is how they get the sum at the end.
Patrice T 23-Mar-21 9:50am    
Sum of all taken values.
Richard MacCutchan 23-Mar-21 9:56am    
Quote: We select 4, so 4 and 3 are deleted leaving us with {1,2,2,2}.
Then we select 2, so 2 & 1 are deleted. We are left with{2,2}.
We select 2 in next two steps, thus the sum is 4+2+2+2=10.

Doesn't make sense to me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900