First, take an input of any number by the user after that add all numbers and sort the array after sorting an array remove the first index element and last index element from the array if you get then okay if not see the below full explanation of the problem min-max sum. Read the program statement to find the Mini-Max Sum Hackerrank Solution in C++.
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
arr = [1,3,5,7,9]. Our minimum sum is 1 + 3 + 5 + 7 = 16 and our maximum sum is 3 + 5 + 7 + 9 = 24. The function prints
16 24
Function Description
Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.
miniMaxSum has the following parameter(s):
Print two space-separated integers on one line: the minimum sum and the maximum sum of elements.
Input Format
A single line of five space-separated integers.
Constraints
1<=arr[i]<=10^9
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
Mini-Max Sum Explanation
Our initial numbers are 1, 2, 3, 4, and 5. We can calculate the following sums using four of the five integers:
If we sum everything except 1, our sum is 2+3+4+5=14.
If we sum everything except 2, our sum is 1+3+4+5=13.
If we sum everything except 3, our sum is 1+2+4+5=12.
If we sum everything except 4, our sum is 1+2+3+5=11.
If we sum everything except 5, our sum is 1+2+3+4=10.
Hint: Beware of integer overflow! Use 64-bit Integer.
16 24
Function Description
Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.
miniMaxSum has the following parameter(s):
- arr: an array of 5 integers
Print two space-separated integers on one line: the minimum sum and the maximum sum of elements.
Input Format
A single line of five space-separated integers.
Constraints
1<=arr[i]<=10^9
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
Mini-Max Sum Explanation
Our initial numbers are 1, 2, 3, 4, and 5. We can calculate the following sums using four of the five integers:
If we sum everything except 1, our sum is 2+3+4+5=14.
If we sum everything except 2, our sum is 1+3+4+5=13.
If we sum everything except 3, our sum is 1+2+4+5=12.
If we sum everything except 4, our sum is 1+2+3+5=11.
If we sum everything except 5, our sum is 1+2+3+4=10.
Hint: Beware of integer overflow! Use 64-bit Integer.
Submit your solution here: Click here
Tip: Before copying the program I recommended please read this full article, this will help you to understand the below Mini Max Hackerrank Solution in C++. Solve the problem by using any sorting like- Bubble Sort, Selection Sort, or Insertion Sort.
Mini Max Sum Hackerrank Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int val, i;
long long int sum = 0;
vector<int> array;
for (i = 0; i < 5; i++)
{
cin >> val;
sum += val;
//cout<<sum<<" ";
array.push_back(val);
}
//cout<<endl;
sort(array.begin(), array.end());
/*for(i=0; i < 5; i++)
{
cout<<array[i]<<endl;
}*/
cout << sum - array[4] << " " << sum - array[0];
return 0;
}
The Logic to Mini-Max Sum
The First step is to take input from the user and after that take another variable to add all 5 numbers of an array and store the sum of 5 variables in the sum name variable for better understanding let's take an example to suppose array 5 elements are 2, 5, 1, 4, 3. The logic is very easy to find Mini Max Sum Hackerrank Solution in C++.
for (i = 0; i < 5; i++)
{
cin >> val;
sum += val;
//cout<<sum<<" ";
array.push_back(val);
}
The total sum of 5 elements is = 15
So the sum of the 5 variables in an array is 15, Now next step is to sort the array, for sorting I am using the sort function of a vector by default it sorts an array in ascending order for our array element to become after sorting.
Sort function of a vector
sort(array.begin(), array.end());
After sorting the elements of an array
1, 2, 3, 4, 5
Note: You can use here Bubble Sort, Selection Sort, Insertion Sort and many other Different Types of Sorting.
Now we know that small elements and big elements are in the array so just minus the small and big elements of an array in this way we can get a Min-Max Sum of an array.
Max sum = sum - array[array first index element]
Min-Sum = sum - array[array last index element]
Max sum = 15 - 1 = 14
Min-Sum = 15 - 5 = 10
So hence we get our answer according to problem statements in hackerrank.
Mini-Max Sum Hackerrank Solution in C++ Output
Similar to Mini Max
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
- Inheritance Introduction Hackerrank Solution in C++
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
- For Loop Hackerrank Solution in C++
- Functions in C++ Hackerrank Solution
- Pointer Hackerrank Solution in C++
- Arrays Introduction Hackerrank Solution in C++
- Strings Hackerrank Solution in C++
- Plus Minus Hackerrank Solution C++
- Hello World in C Hackerrank Solution
- Divisible Sum Pairs Hackerrank Solution in C++
- Apple And Orange Hackerrank Solution
- Simple Array Sum Hackerrank Solution C++
- Compare The Triplets Hackerrank Solution C++
0 Comments: