> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/binary-search/bs-on-2d-arrays/2643.-row-with-maximum-ones.md).

# 2643. Row With Maximum Ones

## Problem Statement

<br>

Given a `m x n` binary matrix `mat`, find the **0-indexed** position of the row that contains the **maximum** count of **ones,** and the number of ones in that row.

In case there are multiple rows that have the maximum count of ones, the row with the **smallest row number** should be selected.

Return *an array containing the index of the row, and the number of ones in it.*

&#x20;

**Example 1:**

<pre><code><strong>Input: mat = [[0,1],[1,0]]
</strong><strong>Output: [0,1]
</strong><strong>Explanation: Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1). So, the answer is [0,1]. 
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: mat = [[0,0,0],[0,1,1]]
</strong><strong>Output: [1,2]
</strong><strong>Explanation: The row indexed 1 has the maximum count of ones (2). So we return its index, 1, and the count. So, the answer is [1,2].
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: mat = [[0,0],[1,1],[0,0]]
</strong><strong>Output: [1,2]
</strong><strong>Explanation: The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
</strong></code></pre>

&#x20;

**Constraints:**

* `m == mat.length`&#x20;
* `n == mat[i].length`&#x20;
* `1 <= m, n <= 100`&#x20;
* `mat[i][j]` is either `0` or `1`.

## Intuition

```
Find using BS
```

### Links

<https://leetcode.com/problems/row-with-maximum-ones/description/>

### Video Links

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int find_zeros(vector<int>& arr){
        int n = arr.size();
        int low=0, high=arr.size()-1;
        sort(arr.begin(), arr.end());

        while(low<=high){
            int mid = low+(high-low)/2;
            if(arr[mid] == 0)
                low = mid+1;
            else
                high = mid-1;
        }

        return low == n ? 0 : n-low;
    }

    vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
        int max_zero=-1, index=-1;

        for(int i=0; i<mat.size(); i++){
            int row_zero = find_zeros(mat[i]);
            if(row_zero > max_zero){
                max_zero = row_zero;
                index = i;
            }
        }

        return {index, max_zero};
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
