Given a boolean 2D array where each row is sorted. Find the row with the maximum number of 1s. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Saturday 4 July 2020

Given a boolean 2D array where each row is sorted. Find the row with the maximum number of 1s.

Given a boolean 2D array where each row is sorted. Find the row with the maximum number of 1s.

Given a boolean 2D array where each row is sorted. Find the row with the maximum number of 
1s.

Example:
Input:
2
4 4
0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0
2 2
0 0 1 1

Output:
2
1

#include <iostream>

using namespace std;

int main() {
    //code
    int t;
    cin >> t;
    while (t--) {
        int n, m, l = 0, cnt = 0, ans = 0, x;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            cnt = 0;
            for (int j = 0; j < m; ++j) {
                cin >> x;
                if (x == 1) {
                    cnt++;
                }
            }
            if (cnt > l) {
                ans = i;
                l = cnt;
            }
        }
        cout << ans << endl;

    }

    return 0;
}