반응형

Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.


You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.


Example


For a = [50, 60, 60, 45, 70], the output should be


alternatingSums(a) = [180, 105].


code>>


std::vector<int> alternatingSums(std::vector<int> a) {

    

    std::vector<int> result;

    result.push_back(0);

    result.push_back(0);

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

    {

        result[i%2] += a[i];

    }

    return result;

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

16>areSimilar  (0) 2017.06.28
15>addBorder  (0) 2017.06.28
13>reverseParentheses  (0) 2017.06.28
12>sortByHeight  (0) 2017.06.28
11>isLucky  (0) 2017.06.28

+ Recent posts