429. Find Smallest Common Element in All Rows

Given a matrix mat where every row is sorted in strictly increasing order, return the smallest common element in all rows.

If there is no common element, return -1.

Example 1:

Input: mat = [ [1, 2, 3, 4, 5], [2, 4, 5, 8, 10], [3, 5, 7, 9, 11], [1, 3, 5, 7, 9] ]

Output: 5

Explanation: Smallest Common Element in all rows is 5.

Example 2:

Input: mat = [ [1, 2, 3], [2, 3, 4], [2, 3, 5] ]

Output: 2

Explanation: Smallest Common Element in all rows is 2.

Now Your Turn!

Pick the correct output for the given input

Input: mat = [ [1, 2, 3, 4, 5], [2, 3, 5, 7, 11], [2, 3, 5, 13, 17] ]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= mat.length, mat[i].length <= 500
  • 1 <= mat[i][j] <= 104
  • mat[i] is sorted in strictly increasing order.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int smallestCommonElement(vector<vector<int>>& mat) {
//Your Code Goes Here
}
};
Test Case

Input:

Mat