Ctrl K

A Value Shared by Every Row

Medium
You are given a table grid where every row is strictly increasing. Find the smallest number that appears in every single row. If no number is shared by all rows, return -1.
Example 1
grid=[[2, 4, 6, 8], [1, 4, 8, 9], [4, 5, 8, 10]] → 4

All three rows contain 4 and 8; the smaller of the two is 4.

Example 2
grid=[[1, 3], [2, 4]] → -1

The rows share no number at all.

Constraints
  • 1 ≤ number of rows ≤ 500
  • 1 ≤ row length ≤ 500, all rows have the same length
  • 1 ≤ grid[i][j] ≤ 10⁴
  • Every row is strictly increasing
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCleanup by Unequal Pairs MediumPrimes Along Grid Rays MediumPaint a Y on the Board MediumSpells Without Neighbors MediumCommon to Three Sorted Arrays Easy
loading Python…
Input
grid=[[2, 4, 6, 8], [1, 4, 8, 9], [4, 5, 8, 10]]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 90 tests on the server, hidden ones included.