Mini max sum Hackerrank solution in c, c++, python , java 



Problem statement : 

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Example

The minimum sum is  and the maximum sum is . The function prints

16 24

Function Description

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

  • arr: an array of  integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of  of  elements.

Input Format

A single line of five space-separated integers.

Constraints

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

The numbers are , and . Calculate the following sums using four of the five integers:

  1. Sum everything except , the sum is .
  2. Sum everything except , the sum is .
  3. Sum everything except , the sum is .
  4. Sum everything except , the sum is .
  5. Sum everything except , the sum is .

Solution :

Code in C :

#include<stdio.h>
 int main ()
 {
      long int  num[5],sum[5]={ 0,0,0,0,0} ,max=0, min;
      int i,j;
      for ( i = 0; i < 5; i++)
      {
          scanf("%ld",&num[i]);
      }
      for ( i = 0; i < 5; i++)
      {
          for ( j = 0; j < 5; j++)
          {
              if ( i!= j )
              {
                  sum[i]=sum[i]+ num[j];
              }
              
          }
          if ( sum[i]>= max )
          {
              max=sum[i];
          }
       
      }
        min=max;
      for ( i = 0; i < 5; i++)
      {
           
          if ( sum[i]<= min )
          {
              min=sum[i];
          }
      }
      
      printf("%ld %ld",min,max);
      
      
 }

Code in C++:

#include<iostream>
using namespace std ;
 int main ()
 {
      long int  num[5],sum[5]={ 0,0,0,0,0} ,max=0min;
      int i,j;
      for ( i = 0i < 5i++)
      {
          cin>>num[i];
      }
      for ( i = 0i < 5i++)
      {
          for ( j = 0j < 5j++)
          {
              if ( i!= j )
              {
                  sum[i]=sum[i]+ num[j];
              }
              
          }
          if ( sum[i]>= max )
          {
              max=sum[i];
          }
       
      }
        min=max;
      for ( i = 0i < 5i++)
      {
           
          if ( sum[i]<= min )
          {
              min=sum[i];
          }
      }
      
      cout<<min<<" "<<max;
          
 }

Code in python :

a = input().strip().split(' ')
for i in range(0len(a)):
    a[i] = int(a[i])
    
s = sum(a)
print(str(s - max(a)) + " " + str(s - min(a)))

code in java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long sum = 0;
        long max = Long.MIN_VALUE;
        long min = Long.MAX_VALUE;
        for (int i = 0; i < 5; i++){
            long n = in.nextLong();
            sum += n;
            max = Math.max(max, n);
            min = Math.min(min, n);
        }
        System.out.println((sum - max) + " " + (sum - min));
    }
}


Please comment if you have any doubt or for any other problem .
Thanks