Thursday, August 18, 2022
HomeWordPress DevelopmentDiscover row and column pair in given Matrix with equal row and...

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


#embody <bits/stdc++.h>

utilizing namespace std;

  

vector<pair<int, int> > equalPairs(vector<vector<int> >& M)

{

    vector<pair<int, int> > ans;

    int R = M.measurement(), C = M[0].measurement();

    vector<int> arrR(R), arrC(C);

  

    

    for (int i = 0; i < R; ++i) {

        int s = 0;

        for (int j = 0; j < C; ++j)

            s += M[i][j];

        arrR[i] = s;

    }

  

    

    for (int j = 0; j < C; ++j) {

        int s = 0;

        for (int i = 0; i < R; ++i)

            s += M[i][j];

        arrC[j] = s;

    }

  

    

    

    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;

}

  

int most important()

{

    vector<vector<int> > M{ { 1, 2, 2 },

                            { 1, 5, 6 },

                            { 3, 8, 9 } };

  

    

    vector<pair<int, int> > ans = equalPairs(M);

    if (ans.measurement() == 0)

        cout << "No such pairs exists";

    else {

        for (int i = 0; i < ans.measurement(); i++)

            cout << "{" << ans[i].first << ", "

                 << ans[i].second << "}n";

    }

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments