Sum of Even and Odd Numbers Separately

Medium ⏱ 12 min 64% acceptance ★★★★★ 4.9
Write a function sum_even_odd(nums) that returns a tuple (even_sum, odd_sum) — the sum of all even numbers and the sum of all odd numbers in the list nums — computed in a single pass with one loop and two accumulators.

Examples

Example 1
Input
sum_even_odd([1, 2, 3, 4, 5, 6])
Output
(12, 9)
Explanation

Evens 2+4+6=12; odds 1+3+5=9.

Example 2
Input
sum_even_odd([])
Output
(0, 0)
Explanation

Empty list sums to zero on both.

Constraints

  • nums is a list of integers.

Topics

Loopsfor loopaccumulator

Companies

AmazonRazorpay

Hints

Hint 1

Initialize two accumulators to 0 before the loop.

Hint 2

Branch on `n % 2 == 0` inside the loop to decide which accumulator to update.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.