insidcode
ProblemsDuelLeaderboardStats
Sign In
  1. Home
  2. Problems
  3. Search for a target value in a row-wise and column-wise sorted 2D matrix in \mathcal{O}(M + N) time.

Search for a target value in a row-wise and column-wise sorted 2D matrix in \mathcal{O}(M + N) time.

Difficulty: Hard

Phase: 6 · Mixed Logical Challenges

XP Reward: 30 XP

Topics: Mixed Logical Challenges, Hard

Supported Languages: Python, JavaScript, C, C++, Java

Problem Description

Given R and C followed by R x C matrix sorted row-wise and column-wise, and target X on next line, check if X exists in O(M+N) time. Print 'Yes' or 'No'. Input Format: Read input from standard input (stdin). Output Format: Print the result to standard output (stdout).

Constraints

  • -10^9 <= N <= 10^9

Examples

Example 1

Input: 3 3 1 4 7 2 5 8 3 6 9 5

Output: Yes

Explanation: Sample execution for input 3 3 1 4 7 2 5 8 3 6 9 5