Skip to content
Related Articles

Related Articles

Find row and column pair in given Matrix with equal row and column sum

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 30 Aug, 2022
View Discussion
Improve Article
Save Article

Given a matrix Mat of size N x M, the task is to find all the pairs of rows and columns where the sum of elements in the row is equal to the sum of elements in the columns.

Examples:

Input: M = {{1, 2, 2}, {1, 5, 6}, {3, 8, 9}}
Output: {{1, 1}}
Explanation: The sum of elements of rows and columns of matrix M are:

 C = 1C = 2C = 3Sum of Rows
R = 11225
R = 215612
R = 338920
Sum of Columns51517 

Thus, row 1 and columns 1 has same sum.

Input: M = {{1, 2, 3, 4}, {2, 5, 8, 7}, {3, 8, 9, 3}, {0, 6, 3, 2}}
Output: {{3, 3}}

Approach: To solve the problem follow the below idea:

The idea is to calculate the sum of each row and store them in an array, do the same for each column. Compare values from these arrays and return the result.

Follow the steps to solve this problem:

  • Declare to arrays, arrR[] to store the sums of each row, and arrC[] to store the sums of each column.
  • Using nested loops calculate the sums of each row and store the values in arrR[].
  • Using nested loops calculate the sums of each column and store the values in arrC[].
  • Use the nested loops to check if for any pair of rows and columns the sums are equal and store them if possible.

Below is the implementation of this approach:

C++




// C++ code to implement this approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the pairs of
// rows and columns with equal sum
vector<pair<int, int> > equalPairs(vector<vector<int> >& M)
{
    vector<pair<int, int> > ans;
    int R = M.size(), C = M[0].size();
    vector<int> arrR(R), arrC(C);
 
    // Calculate the sum of each row
    for (int i = 0; i < R; ++i) {
        int s = 0;
        for (int j = 0; j < C; ++j)
            s += M[i][j];
        arrR[i] = s;
    }
 
    // Calculate the sum of each column
    for (int j = 0; j < C; ++j) {
        int s = 0;
        for (int i = 0; i < R; ++i)
            s += M[i][j];
        arrC[j] = s;
    }
 
    // Check whether any pair of row and
    // column have equal sum of elements
    for (int i = 0; i < R; ++i) {
        for (int j = i; j < C; ++j) {
            if (arrR[i] == arrC[j])
                ans.push_back({ i + 1, j + 1 });
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    vector<vector<int> > M{ { 1, 2, 2 },
                            { 1, 5, 6 },
                            { 3, 8, 9 } };
 
    // Function call
    vector<pair<int, int> > ans = equalPairs(M);
    if (ans.size() == 0)
        cout << "No such pairs exists";
    else {
        for (int i = 0; i < ans.size(); i++)
            cout << "{" << ans[i].first << ", "
                 << ans[i].second << "}\n";
    }
    return 0;
}

Java




// Java code for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    static class pair {
        int first, second;
        public pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
    }
 
    // Function to return the pairs of rows and columns with
    // equal sum
    static List<pair> equalPairs(int[][] M)
    {
        List<pair> ans = new ArrayList<>();
        int R = M.length, C = M[0].length;
        int[] arrR = new int[R];
        int[] arrC = new int[C];
 
        // Calculate the sum of each row
        for (int i = 0; i < R; ++i) {
            int s = 0;
            for (int j = 0; j < C; ++j)
                s += M[i][j];
            arrR[i] = s;
        }
 
        // Calculate the sum of each column
        for (int j = 0; j < C; ++j) {
            int s = 0;
            for (int i = 0; i < R; ++i)
                s += M[i][j];
            arrC[j] = s;
        }
 
        // Check whether any pair of row and
        // column have equal sum of elements
        for (int i = 0; i < R; ++i) {
            for (int j = i; j < C; ++j) {
                if (arrR[i] == arrC[j])
                    ans.add(new pair(i + 1, j + 1));
            }
        }
        return ans;
    }
 
    public static void main(String[] args)
    {
        int[][] M = new int[][] { { 1, 2, 2 },
                                  { 1, 5, 6 },
                                  { 3, 8, 9 } };
 
        // Function call
        List<pair> ans = equalPairs(M);
        if (ans.size() == 0) {
            System.out.print("No such pairs exists");
        }
        else {
            for (int i = 0; i < ans.size(); i++) {
                pair temp = (pair)ans.get(i);
                System.out.println("{" + temp.first + ","
                                   + temp.second + "}");
            }
        }
    }
}
 
// This code is contributed by lokeshmvs21.

Output

{1, 1}

Time Complexity: O(R*C)
Auxiliary Space: O(max(R, C))


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!