Easy 0 solved 0% acceptance

Row Sums

Add up every row of a grid.

What you'll learn

  • Loop over the rows of a grid
  • Sum the values in each row
  • Return one total per row

Problem

Return the sum of each row of the grid, in row order.

Implement row_sums(grid).

Input

grid: A list of rows of integers.

Output

Return one total per row.

Constraints

0 ≤ rows ≤ 1,000; 0 ≤ values per row ≤ 1,000

Examples

Example 1
Input:
grid = [
    [1, 2, 3],
    [4, 5, 6]
]

Expected output:
expected = [6, 15]
Input:
std::vector<std::vector<long long>> grid = {
    {1, 2, 3},
    {4, 5, 6}
};

Expected output:
std::vector<long long> expected = {6, 15};
Input:
long[][] grid = new long[][] {
    new long[] {1, 2, 3},
    new long[] {4, 5, 6}
};

Expected output:
long[] expected = new long[] {6, 15};

Explanation

1+2+3 = 6 and 4+5+6 = 15: [6, 15].

Example 2
Input:
grid = [
    [],
    [7]
]

Expected output:
expected = [0, 7]
Input:
std::vector<std::vector<long long>> grid = {
    {},
    {7}
};

Expected output:
std::vector<long long> expected = {0, 7};
Input:
long[][] grid = new long[][] {
    new long[] {},
    new long[] {7}
};

Expected output:
long[] expected = new long[] {0, 7};

Explanation

An empty row adds up to 0: [0, 7].

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Each output value comes from one row of the grid.

Hint 2

Sum the numbers in each row independently and keep the results in order.

Hint 3

An empty grid should produce an empty result.

Expected complexity

O(total values) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.