Sunday, 2 April 2017

HackerRank Solution For Mini-Max Sum in C++

Problem:- Hacker Rank Solution Program In C++ For " Mini-Max Sum " or Hacker Rank Solution Program In C++ For Mini-Max Sum or Mini-Max Sum solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for Mini-Max Sum subdomain or Mini-Max Sum solution in c++ of hacker rank.


Logic:- This min-max sum problem is very popular and important in hacker rank. In min-max sum problem, we have 5 number as input and we have to find the sum of 4 elements in this way that 4 number out of 5 number sum should be maximum and same for a minimum sum of an array. I am going to tell to problem statement step by step with full explanation trust me this is the simplest problem. First, take an input of any number by user after that add all number and sort the array and after sorting an array remove first index element and last index element from an array if you get than ok if not see the below full explanation of the problem min-max sum 


Explanation:- The First step is to take an input from user and after that take another variable add the all 5 number of an array and store the sum of 5 variable in sum name variable for better understanding let's take an example suppose array 5 elements are 2, 5, 1, 4, 3.

for(i=0; i<5; i++)
{
        cin>>val;
        sum+=val;
        //cout<<sum<<" ";
        array.push_back(val);
}

Total sum of 5 elements is = 15

So the sum of the 5 variables in an array is 15, Now next step to sort the array, for sorting I am using sort function of a vector by default it sorts an array ascending order for our array element become after sorting.

Sort function of a vector 

sort(array.begin(), array.end());

After sorting element of an array

1, 2, 3, 4, 5 

Note:- You can use here Bubble Sort, Selection Sort, Insertion Sort and many others

Now we know that small element and big element in the array so just minus the small and big element 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 hacker rank.

Check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.

Submit your solution here:- Click here

Tip:- Before copy the program I recommended please read this full article, this will help you to build your own logic. Solve the problem by using above any sorting like- Bubble Sort, Selection Sort, Insertion Sort.

Solution:-

#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;
}

Output:-

Hacker Rank Solution For " Mini-Max Sum " in C++

Hacker Rank Solution For Mini-Max Sum in C++



You May Also Like:-

1. C Program For Find A Grade Of Given Marks Using Switch Case

2. C++ Program For Store Employee Information And Display Using Structure

3. C Program For Finding Radius Circumference Using Switch Case

4. C Program For Calculator Using Switch Case

5. Java Program For Find The Gross Salary Of An Employee

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Calculate Percentage Of 5 Subjects

8. Hacker Rank Solution For Strings

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment