Flood Fill Algorithm Task

Assist Ninja in altering the color of a specific region in his photo. Given the image as a 2D array where each pixel is a positive integer, update the color of a specified pixel and the adjacent pixels with the same color to a new color.

Explanation:

The task requires changing the color of a given pixel located at coordinates (row, column) and its directly adjacent same-colored pixels (up, down, left, right; not diagonally) in the image to a new specified color.

Input:

M (rows in image), N (columns), and the image matrix of dimensions MxN.
Coordinates X, Y (row and column of the pixel) and the new color C.

Output:

Print the updated image matrix after applying the new color.

Example:

Consider a 4x4 image matrix. Starting pixel is located at (1, 2) with new color 8. Original value is 3. The new image shows altered pixels.

Original: [[1, 1, 1, 1],
[1, 3, 3, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
New: [[1, 1, 1, 1],
[1, 8, 8, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]

Constraints:

  • 1 <= T <= 100
  • 1 <= M, N <= 50
  • 0 <= X < M
  • 0 <= Y < N
  • 1 <= Image[i][j], C <= 105
  • Time Limit: 1 second
Be the first one to answer
Add answer anonymously...
Bajaj Finserv Software Developer 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