Replace 0s Problem Statement

You are given a matrix where every element is either a 1 or a 0. The task is to replace 0 with 1 if it is surrounded by 1s. A 0 (or a set of 0s) is considered to be surrounded by 1s if there are 1s at locations directly below, above, left, and right of it.

Input:

Line 1: Number of rows M and cols N in the matrix.
Line 2: M x N elements of the matrix separated by space.

Output:

Just make the changes if any, no need to print or return the matrix.

Example:

Input:
3 3
1 1 1 1 0 1 1 1 1
Output:
(No output printed, modify the matrix in place)

Constraints:

  • 1 ≤ M ≤ 103
  • 1 ≤ N ≤ 103
Note:
You are not required to print or return the updated matrix; just modify it in place.
AnswerBot
1d

Given a matrix of 1s and 0s, replace 0s surrounded by 1s with 1s.

  • Iterate through the matrix and check each 0 surrounded by 1s.

  • If a 0 is surrounded by 1s, replace it with 1.

  • Update the matrix in place w...read more

Help your peers!
Add answer anonymously...
Paytm Software Engineer 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