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
4mo

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!
Select
Add answer anonymously...

Cult.fit Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Pair with Given Sum in a Balanced BST Problem Statement You are given the ‘root’...read more
A Software Developer Intern was asked Q. Break The Board Problem Statement Your task is to break a board of given dimensi...read more
A Software Developer Intern was asked Q. Word Break Problem Statement You are given a list of N strings called A. Your ta...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Pair with Given Sum in a Balanced BST Problem Statement You are given the ‘root’...read more
A Software Developer Intern was asked Q2. Break The Board Problem Statement Your task is to break a board of given dimensi...read more
A Software Developer Intern was asked Q3. Word Break Problem Statement You are given a list of N strings called A. Your ta...read more
Cult.fit Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits