HackerEarth - Maximum Sum Solution
1 min read

HackerEarth - Maximum Sum Solution

HackerEarth - Maximum Sum Solution

You are given an array of integers A, you need to find the maximum sum that can be obtained by picking some non-empty subset of the array. If there are many such non-empty subsets, choose the one with the maximum number of elements. Print the maximum sum and the number of elements in the chosen subset.

Input:

The first line contains an integer N, denoting the number of elements of the array. The next line contains N space-separated integers, denoting the elements of the array.

Output:

Print 2 space-separated integers, the maximum sum that can be obtained by choosing some subset and the maximum number of elements among all such subsets which have the same maximum sum.

Constraints:

1≤N≤105

−109≤Ai≤109

SAMPLE INPUT

5
1 2 -4 -2 3

SAMPLE OUTPUT

6 3

Explanation

The chosen subset is {1, 2, 3}.

Solution in Python

n = int(input())
a = list(map(int,input().split()))
m = [i for i in a if i>=0] or [max(i for i in a if i<0)]
print(sum(m),len(m))

Explanation

The sum of all the values greater than or equal to 0 is our required value.

However all numbers might be negative in some cases. In that case we will choose the greatest number among all the negative values.

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×