Vertical Sum in a Jagged 2D Array

Last Updated : 1 Aug, 2026

Given a jagged 2D array arr[][], where each row may contain a different number of elements, find the minimum sum among all columns.

Examples:

Input: arr[][] = [[2, 3, 5], [1, 2], [1, 4, 5, 1]]
Output: 1
Explanation: The column sums are 4 (2 + 1 + 1), 9 (3 + 2 + 4), 10 (5 + 5), and 1. Hence, the minimum column sum is 1.

Input: arr[][] = [[1, 2, 3, 4], [3, 3], [1, 2, 5, 9]]
Output: 5
Explanation: The column sums are 5 (1 + 3 + 1), 7 (2 + 3 + 2), 8 (3 + 5), and 13 (4 + 9). Hence, the minimum column sum is 5.

Try It Yourself
redirect icon

[Naive Approach] Traverse Each Column Separately - O(n * m) Time and O(1) Space

Since we need the minimum sum among all columns, the idea is to process each column independently. First, determine the maximum number of columns present in the jagged array. Then, for every column, traverse all rows and add the elements that exist in that column. Finally, return the smallest column sum.

  • Find the maximum number of columns present in any row.
  • Initialize the answer as a large value.
  • Traverse each column from 0 to maxCols - 1.
  • For every column, iterate through all rows and add the element if the column exists in that row.
  • Update the minimum column sum.
  • Return the minimum column sum.
C++
#include <bits/stdc++.h>
using namespace std;

int minSum(vector<vector<int>> &arr)
{
    // Find the maximum number of columns.
    int maxCols = 0;
    for (auto &row : arr)
        maxCols = max(maxCols, (int)row.size());

    int ans = INT_MAX;

    // Traverse each column separately.
    for (int col = 0; col < maxCols; col++)
    {
        // Stores the sum of the current column.
        int currSum = 0;

        // Add all elements present in the current column.
        for (auto &row : arr)
        {
            if (col < (int)row.size())
                currSum += row[col];
        }

        // Update the minimum column sum.
        ans = min(ans, currSum);
    }

    return ans;
}

int main()
{
    vector<vector<int>> arr = {{2, 3, 5}, {1, 2}, {1, 4, 5, 1}};
    cout << minSum(arr) << endl;

    return 0;
}
Java
import java.util.*;

public class GFG {
    static int minSum(int[][] arr)
    {
        // Find the maximum number of columns.
        int maxCols = 0;
        for (int[] row : arr)
            maxCols = Math.max(maxCols, row.length);

        int ans = Integer.MAX_VALUE;

        // Traverse each column separately.
        for (int col = 0; col < maxCols; col++) {

            // Stores the sum of the current column.
            int currSum = 0;

            // Add all elements present in the current
            // column.
            for (int[] row : arr) {
                if (col < row.length)
                    currSum += row[col];
            }

            // Update the minimum column sum.
            ans = Math.min(ans, currSum);
        }

        return ans;
    }

    public static void main(String[] args)
    {
        int[][] arr
            = { { 2, 3, 5 }, { 1, 2 }, { 1, 4, 5, 1 } };

        System.out.println(minSum(arr));
    }
}
Python
def minSum(arr):

    # Find the maximum number of columns.
    max_cols = 0
    for row in arr:
        max_cols = max(max_cols, len(row))

    ans = float('inf')

    # Traverse each column separately.
    for col in range(max_cols):

        # Stores the sum of the current column.
        curr_sum = 0

        # Add all elements present in the current column.
        for row in arr:
            if col < len(row):
                curr_sum += row[col]

        # Update the minimum column sum.
        ans = min(ans, curr_sum)

    return ans


# Driver Code
if __name__ == "__main__":
    arr = [
        [2, 3, 5],
        [1, 2],
        [1, 4, 5, 1]
    ]

    print(minSum(arr))
C#
using System;

class GFG {
    static int minSum(int[][] arr)
    {
        // Find the maximum number of columns.
        int maxCols = 0;
        foreach(int[] row in arr) maxCols
            = Math.Max(maxCols, row.Length);

        int ans = int.MaxValue;

        // Traverse each column separately.
        for (int col = 0; col < maxCols; col++) {
            // Stores the sum of the current column.
            int currSum = 0;

            // Add all elements present in the current
            // column.
            foreach(int[] row in arr)
            {
                if (col < row.Length)
                    currSum += row[col];
            }

            // Update the minimum column sum.
            ans = Math.Min(ans, currSum);
        }

        return ans;
    }

    static void Main()
    {
        int[][] arr
            = { new int[] { 2, 3, 5 }, new int[] { 1, 2 },
                new int[] { 1, 4, 5, 1 } };

        Console.WriteLine(minSum(arr));
    }
}
JavaScript
function minSum(arr)
{
    // Find the maximum number of columns.
    let maxCols = 0;
    for (const row of arr)
        maxCols = Math.max(maxCols, row.length);

    let ans = Number.MAX_SAFE_INTEGER;

    // Traverse each column separately.
    for (let col = 0; col < maxCols; col++) {

        // Stores the sum of the current column.
        let currSum = 0;

        // Add all elements present in the current column.
        for (const row of arr) {
            if (col < row.length)
                currSum += row[col];
        }

        // Update the minimum column sum.
        ans = Math.min(ans, currSum);
    }

    return ans;
}

// Driver Code
const arr = [ [ 2, 3, 5 ], [ 1, 2 ], [ 1, 4, 5, 1 ] ];

console.log(minSum(arr));

Output
1

[Expected Approach] Traverse Each Element Once - O(No. of Elements) Time and O(m) Space

Instead of traversing each column separately, we can process every element exactly once while traversing the rows. We maintain an array where each index stores the sum of the corresponding column. As we visit an element, we simply add it to its column's sum. After processing all elements, the minimum value in this array is the required answer.

  • Find the maximum number of columns present in any row.
  • Create a colSum array of size maxCols and initialize it with 0.
  • Traverse each row of the jagged array.
  • For every element in the current row, add its value to the corresponding column sum.
  • Traverse the colSum array to find the minimum column sum.
  • Return the minimum column sum.
C++
#include <bits/stdc++.h>
using namespace std;

int minSum(vector<vector<int>> &arr)
{
    // Find the maximum number of columns.
    int maxCols = 0;
    for (auto &row : arr)
        maxCols = max(maxCols, (int)row.size());

    // Stores the sum of each column.
    vector<int> colSum(maxCols, 0);

    // Compute column-wise sums.
    for (auto &row : arr)
    {
        for (int col = 0; col < (int)row.size(); col++)
        {
            colSum[col] += row[col];
        }
    }

    // Find the minimum column sum.
    int ans = INT_MAX;
    for (int sum : colSum)
        ans = min(ans, sum);

    return ans;
}

int main()
{
    vector<vector<int>> arr = {{2, 3, 5}, {1, 2}, {1, 4, 5, 1}};
    cout << minSum(arr) << endl;

    return 0;
}
Java
public class GFG {
    static int minSum(int[][] arr)
    {
        // Find the maximum number of columns.
        int maxCols = 0;
        for (int[] row : arr)
            maxCols = Math.max(maxCols, row.length);

        // Stores the sum of each column.
        int[] colSum = new int[maxCols];

        // Compute column-wise sums.
        for (int[] row : arr) {
            for (int col = 0; col < row.length; col++) {
                colSum[col] += row[col];
            }
        }

        // Find the minimum column sum.
        int ans = Integer.MAX_VALUE;
        for (int sum : colSum)
            ans = Math.min(ans, sum);

        return ans;
    }

    public static void main(String[] args)
    {
        int[][] arr
            = { { 2, 3, 5 }, { 1, 2 }, { 1, 4, 5, 1 } };

        System.out.println(minSum(arr));
    }
}
Python
def minSum(arr):

    # Find the maximum number of columns.
    max_cols = 0
    for row in arr:
        max_cols = max(max_cols, len(row))

    # Stores the sum of each column.
    col_sum = [0] * max_cols

    # Compute column-wise sums.
    for row in arr:
        for col in range(len(row)):
            col_sum[col] += row[col]

    # Find the minimum column sum.
    ans = min(col_sum)

    return ans


# Driver Code
if __name__ == "__main__":
    arr = [
        [2, 3, 5],
        [1, 2],
        [1, 4, 5, 1]
    ]

    print(minSum(arr))
C#
using System;

class GFG {
    static int minSum(int[][] arr)
    {
        // Find the maximum number of columns.
        int maxCols = 0;
        foreach(int[] row in arr) maxCols
            = Math.Max(maxCols, row.Length);

        // Stores the sum of each column.
        int[] colSum = new int[maxCols];

        // Compute column-wise sums.
        foreach(int[] row in arr)
        {
            for (int col = 0; col < row.Length; col++) {
                colSum[col] += row[col];
            }
        }

        // Find the minimum column sum.
        int ans = int.MaxValue;
        foreach(int sum in colSum) ans = Math.Min(ans, sum);

        return ans;
    }

    static void Main()
    {
        int[][] arr
            = { new int[] { 2, 3, 5 }, new int[] { 1, 2 },
                new int[] { 1, 4, 5, 1 } };

        Console.WriteLine(minSum(arr));
    }
}
JavaScript
function minSum(arr)
{
    // Find the maximum number of columns.
    let maxCols = 0;
    for (const row of arr)
        maxCols = Math.max(maxCols, row.length);

    // Stores the sum of each column.
    const colSum = new Array(maxCols).fill(0);

    // Compute column-wise sums.
    for (const row of arr) {
        for (let col = 0; col < row.length; col++) {
            colSum[col] += row[col];
        }
    }

    // Find the minimum column sum.
    let ans = Number.MAX_SAFE_INTEGER;
    for (const sum of colSum)
        ans = Math.min(ans, sum);

    return ans;
}

// Driver Code
const arr = [ [ 2, 3, 5 ], [ 1, 2 ], [ 1, 4, 5, 1 ] ];

console.log(minSum(arr));

Output
1
Comment