DSA Tracker

Easy

Row with Maximum 1s

An easy Binary Search problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Binary Search
Sheets
1
Core for
7 roles
Platform
GeeksforGeeks

The problem

Given a row-sorted binary matrix where each row has 1s followed by 0s (or all 1s or all 0s), return the index of the row with the maximum number of 1s. If there is a tie, return the smallest index. If no row contains 1s, return -1.

Example 1

Input
matrix = [[0,0,0,1],[0,0,1,1],[0,1,1,1]]
Output
2
Why
Row 2 has the most 1s (three 1s). Row 0 has one 1 and row 1 has two.

Example 2

Input
matrix = [[0,0],[0,0]]
Output
-1
Why
No row contains any 1s, so return -1.

Constraints

  • 1 <= matrix.length, matrix[i].length <= 100
  • matrix[i][j] is 0 or 1
  • Each row is sorted in non-increasing order

How to think about it

Updated 2026-09-09

Because each row is non-increasing, 1s cluster on the left and transition to 0s. The number of 1s in a row is determined by finding the last index of 1, or by walking a column pointer leftward across rows so work is never repeated.

Approaches, worst first

  1. Count every cell

    time O(m * n) · space O(1)

    Iterate through every row and column, accumulating the count of 1s in each row. Exhaustive and simple, but visits every entry regardless of row ordering.

  2. Binary search per row

    time O(m log n) · space O(1)

    Binary search on each row independently to find the last 1 or the transition to 0. Computes exact counts in logarithmic time per row.

  3. Top-right staircase walkWrite this one

    time O(m + n) · space O(1)

    Start at the top row and move left while cells are 1, then step down when a 0 is encountered. The column pointer only moves left across the entire traversal, keeping the best row index updated.

Where people lose marks · 3
  • Returning the last tied row index instead of the smallest row index when multiple rows have identical counts of 1s.
  • Failing to return -1 when the matrix contains exclusively 0s across all rows.
  • Assuming rows are sorted ascending (0s before 1s) when the problem explicitly specifies non-increasing order (1s followed by 0s).

The theory behind it

Binary Search — the ground this problem stands on. All Binary Search problems

What Binary Search is

Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.

When to reach for it

Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.

How the pattern works

Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.

What each operation costs

OperationTime
find target in sorted arrayO(log n)
find boundary in monotonic rangeO(log n)
What usually goes wrong with Binary Search
  • Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
  • Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
  • Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.

Which roles need this problem

Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.

Companies that have asked it

Tags taken from the problem's own GeeksforGeeks page — not a copied list.

Swiggy

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Binary Search problems

Problem set and role mapping as of .