
Asked in Cadence Design Systems
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 Software Developer Interview Questions Asked at Cadence Design Systems
Q. Given an array, reverse the order of its elements in place.
Q. How would you find the sum of two numbers in a linear array (both sorted and uns...read more
Q. Given the coordinates of a point and a rectangle, how would you determine if the...read more
Interview Questions Asked to Software Developer at Other Companies
Top Skill-Based Questions for Cadence Design Systems Software Developer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

