Mini Max Sum Hackerrank Solution

7 min read

PROBLEM

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.

LINK TO PROBLEM


CODE



#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[5];
long long int total=0;
for(int i=0;i<5;i++){
cin>>arr[i];
total+=arr[i]; //total sum of all five int
}
int* max;
int* min;
max=max_element(arr,arr+5); //find max int in array
min=min_element(arr,arr+5); //find min int in array
long long int maxsum;
long long int minsum;
maxsum=total-*min; // maximum sum of 4 int is =total - min int
minsum=total-*max; //minimum sum of 4 int is =total -max int
cout<<minsum<<" "<<maxsum<<endl;
}


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 .

Hints: Beware of integer overflow! Use 64-bit Integer.

Men' fashion , Dating Tips , Relationship Advice

You may like these posts

  • CODE:  Given an array of integers, find the sum of its elements.For example, if the array , , so return .Function DescriptionComplete the simpleArraySum&n…
  • PROBLEMGiven 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 …
  •  Hacked Exam (8pts, 6pts, 25pts) CODE: In this problem we want to maximize our expected score. Let pqpq be the probability of question qq's answer being&n…
  •  Electronics Shop Hackerrank SolutionCODE A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget. Given pric…

Post a Comment