Given an array of integers numbers, compare the sum of elements on even positions against the sum of elements on odd positions (0-based). Return "even" if the sum of elements on even positions is greater, "odd" if the sum of elements on odd positions is greater, or "equal" if both sums are equal.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(numbers.length^2) will fit within the execution time limit.
Example
For numbers = [1, 2, 3, 4, 5], the output should be solution(numbers) = "even".
Explanation:
Sum of elements on even positions is 1 + 3 + 5 = 9.
Sum of elements on odd positions is 2 + 4 = 6.
9 > 6, so the expected output is "even".
For numbers = [-1, 4, 3, -2], the output should be solution(numbers) = "equal".
Explanation:
Sum of elements on even positions is (-1) + 3 = 2.
Sum of elements on odd positions is 4 + (-2) = 2.
2 = 2, so the expected output is "equal".
这道题要求你按 0-based 下标把数组分成偶数位置和奇数位置两组,分别求和后比较大小,返回 "even"、"odd" 或 "equal"。核心思路很直接:一次遍历数组,遇到偶数下标就累加到偶数和,奇数下标就累加到奇数和,最后做三分判断即可。题目强调时间复杂度不超过 O(n^2),因此 O(n) 的遍历方案最稳妥,也最适合在 OA 中快速实现。