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.
AnswerBot
4mo

Given an array, select elements to maximize sum by removing adjacent elements. Return the maximum sum.

  • Iterate through the array and keep track of the frequency of each element.

  • Select the element with ...read more

Help your peers!
Select
Add answer anonymously...

Cadence Design Systems Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Given an array, reverse the order of its elements in place.
A Software Developer was asked Q. Given the coordinates of a point and a rectangle, how would you determine if the...read more
A Software Developer was asked Q. You are given an array of elements. Some or all of them are duplicates. Find the...read more

Popular interview questions of Software Developer

A Software Developer was asked 6mo agoQ1. Given an array, reverse the order of its elements in place.
A Software Developer was asked Q2. Given the coordinates of a point and a rectangle, how would you determine if the...read more
A Software Developer was asked Q3. You are given an array of elements. Some or all of them are duplicates. Find the...read more
Cadence Design Systems Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits