Friday, 18 November 2016

Mini-Max Sum Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    vector<long int>ar;
    for(int i = 0; i < 5; i++){
        int a;cin >> a;
        ar.push_back(a);
    }
    sort(ar.begin(),ar.end());
    long int mx = 0,min = 0;
    for(int i = 1; i <= 4; i++)mx+=ar[i];
    for(int i = 0; i < 4; i++)min+=ar[i];
    cout << min << " " << mx << endl;
    return 0;
}

2 comments:

  1. Declaration inside the for loop... "int a;" causes error.

    ReplyDelete
  2. this will only work if the given valued are sorted.
    please provide code for unsorted array

    ReplyDelete