Norway


# <iostream>

#include <algorithm>

#include <vector>

using namespace std;

 

// two with by of an array

pair<int,int> maximumSum(vector<int> input)

{

    // sort the array in descending order

    sort(input.rbegin(), input.rend());

 

    // fill x with digits at the odd indices of sorted array

    int x = 0;

    for (int i = 0; i < input.size(); i = i + 2)

        x = x * + input[i];

 

    // fill y with digits at the even indices of sorted array

    int y = 0;

    for (int i = 1; i < input.size(); i = i + 2)

        y = y * 10 + input[i];

 

    // return x and y

    return make_pair(x, y);

}

 

// main function

int main()

{

    vector<int> input = { 4, 6, 2, 7, 9, 8 };

 

    pair<int,int> p = maximumSum(input);

    cout << “The two numbers with maximum sum are “

         << p.first << ” and “ << p.second;

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here