879287. Generate a matrix with each row and column of given sum

Easy

Generate a matrix with each row and column of given sum

Summary

Given a positive integer n and two integers row_sum and col_sum, generate an n x n matrix such that each row's sum is equal to row_sum and each column's sum is equal to col_sum. This problem involves arrays, matrices, and greedy algorithms.

Detailed Explanation

The given problem can be solved by using a greedy approach. We start by initializing the first row of the matrix with values from 1 to row_sum, in such a way that it covers all possible sums up to row_sum.

Then for each subsequent row, we calculate the remaining sum required to reach row_sum and fill the cells accordingly.

For columns, we use a similar approach. We start by initializing the first column with values from 1 to col_sum, in such a way that it covers all possible sums up to col_sum.

Then for each subsequent column, we calculate the remaining sum required to reach col_sum and fill the cells accordingly.

Here is a step-by-step breakdown of the solution:

  1. Initialize an n x n matrix with zeros.
  2. For the first row, generate numbers from 1 to row_sum.
  3. For each subsequent row, calculate the remaining sum required to reach row_sum. Fill the cells accordingly.
  4. Repeat steps 2-3 for columns instead of rows.
  5. Return the generated matrix.

Time complexity: O(n^2) Space complexity: O(n^2)

Optimized Solutions

Java

View on GeeksforGeeks
public class Main {
    public static void main(String[] args) {
        int n = 4;
        int rowSum = 10;
        int colSum = 5;

        int[][] matrix = new int[n][n];

        // Initialize first row and column
        for (int i = 0; i < n; i++) {
            matrix[0][i] = i + 1;
            matrix[i][0] = i + 1;
        }

        // Calculate remaining sums for rows
        int remainingRowSum = rowSum - (n * (n + 1) / 2);
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (remainingRowSum > colSum) {
                    matrix[i][j] = remainingRowSum - (i + j - 1);
                    remainingRowSum -= matrix[i][j];
                } else {
                    matrix[i][j] = colSum;
                    remainingRowSum = 0;
                }
            }
        }

        // Calculate remaining sums for columns
        int remainingColSum = colSum - (n * (n + 1) / 2);
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (remainingColSum > rowSum) {
                    matrix[i][j] = remainingColSum - (i + j - 1);
                    remainingColSum -= matrix[i][j];
                } else {
                    matrix[i][j] = rowSum;
                    remainingColSum = 0;
                }
            }
        }

        // Print the generated matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.