Find Difference Between Sum of All Rows and All Columns in Java


In Java, a matrix can be represented using a two-dimensional array. Matrices are used for storing and manipulating data that have a tabular structure. Matrices have several properties and operations associated with them, such as addition, subtraction, multiplication, transposition, and determinant calculation.

As per the problem statement we have to add sum of all individual rows and columns. Then we need to perform the difference between these added rows and columns and display the result.

Let's start!

For instance

Suppose the original matrix is:

{4, 2, 1},
{3, 5, 6},
{8, 9, 7}

After performing the operation on matrix, the result will be:

Difference between sum of all rows and all columns is: 0

Algorithm

Step-1: Define and initialize the matrix.

Step-2: Calculate the sum of each row and store the results in an array.

Step-3: Calculate the sum of each column and store the results in an array.

Step-4: Calculate the sum of all rows and the sum of all columns.

Step-5: Find the difference between the sum of all rows and the sum of all columns.

Step-6: Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Matrix Elements

  • By Using User Defined Method

Let's see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array Elements

In this approach, matrix elements will be initialized in the program. Then as per the algorithm find the difference between sum of all rows and all columns.

Example

public class Main 
{
   public static void main(String[] args) 
   {
      // Define and initialize the matrix
      int[][] matrix = {{11, 7, 1},{15, 8, 6},{2, 5, 17}};
      // Calculate the sum of each row and store the results in an array
      int[] rowSums = new int[matrix.length];
      for (int i = 0; i < matrix.length; i++) {
         int sum = 0;
         for (int j = 0; j < matrix[i].length; j++) {
            sum += matrix[i][j];
         }
         rowSums[i] = sum;
      }
      // Calculate the sum of each column and store the results in an array
      int[] colSums = new int[matrix[0].length];
      for (int j = 0; j < matrix[0].length; j++) {
         int sum = 0;
         for (int i = 0; i < matrix.length; i++) {
            sum += matrix[i][j];
         }
         colSums[j] = sum;
      }
      // sum of all rows 
      int sumOfRows = 0;
      for (int sum : rowSums) {
         sumOfRows += sum;
      }
      // sum of all columns
      int sumOfCols = 0;
      for (int sum : colSums) {
         sumOfCols += sum;
      }
      // Finding the difference between the sum of all rows and the sum of all columns
      int difference = sumOfRows - sumOfCols;
      // Display the difference
      System.out.println("Difference between sum of all rows and all columns is: " + difference);
   }
}

Output

Difference between sum of all rows and all columns is: 0

Approach-2: By Using User Defined Method

In this approach, mattrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm find the difference between sum of all rows and all columns.

Example

public class Main 
{
   public static void main(String[] args) 
   {
      // Define and initialize the matrix
      int[][] matrix = {{11, 7, 1},{15, 8, 6},{2, 5, 17}};
      // calling user defined method
      callingMatrix(matrix);
   }
   // user defined method	
   public static void callingMatrix(int[][] matrix) 
   {
      // Calculate the sum of each row and store the results in an array
      int[] rowSums = new int[matrix.length];
      for (int i = 0; i < matrix.length; i++) {
         int sum = 0;
         for (int j = 0; j < matrix[i].length; j++) {
            sum += matrix[i][j];
         }
         rowSums[i] = sum;
      }
      // Calculate the sum of each column and store the results in an array
      int[] colSums = new int[matrix[0].length];
      for (int j = 0; j < matrix[0].length; j++) {
         int sum = 0;
         for (int i = 0; i < matrix.length; i++) {
            sum += matrix[i][j];
         }
         colSums[j] = sum;
      }
      // sum of all rows 
      int sumOfRows = 0;
      for (int sum : rowSums) {
         sumOfRows += sum;
      }
      // sum of all columns
      int sumOfCols = 0;
      for (int sum : colSums) {
         sumOfCols += sum;
      }
      // Finding the difference between the sum of all rows and the sum of all columns
      int difference = sumOfRows - sumOfCols;
      // Display the difference
      System.out.println("Difference between sum of all rows and all columns is: " + difference);
   }
}

Output

Difference between sum of all rows and all columns is: 0

In this article, we explored how to find the difference between sum of all rows and all columns by using Java programming language.

Updated on: 17-Aug-2023

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements