Maximum Sum Problem Statement
You are given an array ARR
of N integers. Your task is to perform operations on this array until it becomes empty, and maximize the sum of selected elements. In each operation, select an element from the array at index i
(i.e., ARR[i]
), remove one occurrence of the selected element, and remove all occurrences of ARR[i]-1
and ARR[i]+1
from the array if present.
Example:
Input:
ARR = [2, 3, 3, 3, 4, 5]
Output:
14
Explanation:
Select one of the 3's, removing (3, 2, 4), leaving [3, 3, 5]. Select the remaining 3's, leaving [5]. Select 5, resulting in an empty array. The sum is 3+3+3+5=14.
Constraints:
1 <= T <= 10
1 <= N <= 105
1 <= ARR[i] <= 105
Input:
The first line of input contains an integer 'T' representing the number of test cases.
For each test case:
The first line contains integer N, the size of the array.
The second line contains 'N' space-separated integers representing the array elements.
Output:
For each test case, print a single integer denoting the maximum sum, in a single line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Be the first one to answer
Add answer anonymously...
Top Adobe Member Technical Staff interview questions & answers
Popular interview questions of Member Technical Staff
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app