Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: mat = [[0,1],[1,0]]
Output: [0,1]
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].
Example 2:
Input: mat = [[0,0,0],[0,1,1]] Output: [1,2] 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].
Example 3:
Input: mat = [[0,0],[1,1],[0,0]] Output: [1,2] Explanation: The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
Constraints:
m == mat.length n == mat[i].length 1 <= m, n <= 100 mat[i][j] is either 0 or 1.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[0,1],[1,0]]
[[0,0,0],[0,1,1]]
[[0,0],[1,1],[0,0]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2643: Row With Maximum Ones
class Solution {
public int[] rowAndMaximumOnes(int[][] mat) {
int[] ans = new int[2];
for (int i = 0; i < mat.length; ++i) {
int cnt = 0;
for (int x : mat[i]) {
cnt += x;
}
if (ans[1] < cnt) {
ans[0] = i;
ans[1] = cnt;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2643: Row With Maximum Ones
func rowAndMaximumOnes(mat [][]int) []int {
ans := []int{0, 0}
for i, row := range mat {
cnt := 0
for _, x := range row {
cnt += x
}
if ans[1] < cnt {
ans = []int{i, cnt}
}
}
return ans
}
# Accepted solution for LeetCode #2643: Row With Maximum Ones
class Solution:
def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
ans = [0, 0]
for i, row in enumerate(mat):
cnt = sum(row)
if ans[1] < cnt:
ans = [i, cnt]
return ans
// Accepted solution for LeetCode #2643: Row With Maximum Ones
impl Solution {
pub fn row_and_maximum_ones(mat: Vec<Vec<i32>>) -> Vec<i32> {
let mut ans = vec![0, 0];
for (i, row) in mat.iter().enumerate() {
let cnt = row.iter().sum();
if ans[1] < cnt {
ans = vec![i as i32, cnt];
}
}
ans
}
}
// Accepted solution for LeetCode #2643: Row With Maximum Ones
function rowAndMaximumOnes(mat: number[][]): number[] {
const ans: number[] = [0, 0];
for (let i = 0; i < mat.length; i++) {
const cnt = mat[i].reduce((sum, num) => sum + num, 0);
if (ans[1] < cnt) {
ans[0] = i;
ans[1] = cnt;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.