Water Flow Problem Statement

Given a matrix A with dimensions 'N' x 'M', where each cell represents the height of water in that location, determine the coordinates from which water can flow to both the Pacific and Atlantic oceans. Water can only flow horizontally or vertically to neighboring cells that have a height less than or equal to the current cell.

Explanation:

The matrix boundaries act as ocean borders with the Pacific on the left and top, and the Atlantic on the right and bottom. Find all coordinates where water can flow to both oceans. Return the coordinates in lexicographical order.

Input:

The first line of input contains an integer 'T', the number of test cases.
For each test case, the first line contains two integers, 'N' and 'M', representing the number of rows and columns in the matrix.
The following 'N' lines each contain 'M' space-separated integers representing the matrix 'A'.

Output:

For each test case, the output begins with an integer 'K' representing the number of coordinates.
Each of the next 'K' lines contains the coordinates in the format: row column.

Example:

Input:
1
3 3
1 2 2
3 4 3
2 3 1
Output:
3
0 1
1 1
2 1

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N, M ≤ 104
  • 1 ≤ N*M ≤ 104
  • 1 ≤ A[i][j] ≤ 109
  • Time Limit: 1 second

Note:

Implement the given function. Printing is already handled.
AnswerBot
2d

Given a matrix representing water heights, find coordinates where water can flow to both Pacific and Atlantic oceans.

  • Create a function that performs a depth-first search to find coordinates where wate...read more

Help your peers!
Add answer anonymously...
Cult.fit Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter