HackerEarth - Min-Max Solution
1 min read

HackerEarth - Min-Max Solution

HackerEarth - Min-Max Solution

Given A Series Of N Positive Integers a1,a2,a3........an. , Find The Minimum And Maximum Values That Can Be Calculated By Summing Exactly N-1 Of The N Integers. Then Print The Respective Minimum And Maximum Values As A Single Line Of Two Space-Separated Long Integers.

Input Format

The first line Take Input Value Of N

The second line Take Input N Space Separated Integer Value

Output Format

Two Space Separated Value ( One Maximum Sum And One Minimum Sum )


  • 0 < N < 100001
  • 0 <= ai < 1013


1 2 3 4 5


10 14


Our initial numbers are 1,2,3,4 and 5. We can calculate the following sums using four of the five integers:

  1. If we sum everything except 1, our sum is 2+3+4+5=14.
  2. If we sum everything except 2, our sum is 1+3+4+5=13.
  3. If we sum everything except 3, our sum is 1+2+4+5=12.
  4. If we sum everything except 4, our sum is 1+3+4+5=11.
  5. If we sum everything except 5, our sum is 1+2+3+4=10.

As you can see, the minimal sum is 1+2+3+4=10 and the maximal sum is 2+3+4+5=14. Thus, we print these minimal and maximal sums as two space-separated integers on a new line.

Solution in Python

n = int(input())
a = list(map(int,input().split()))
max_num = max(a)
min_num = min(a)
sum_tot = sum(a)

print(sum_tot-max_num, sum_tot-min_num)

Answer explanation

Max sum is given by total sum - smallest number

Min sum is given by total sum - maximum number

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.