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
AnswerBot
1y

The Flood Fill Algorithm is used to change the color of a particular region in an image and all its adjacent same-colored pixels.

  • The image is represented as a 2D array of positive integers

  • The starting...read more

Anonymous
1y
3
Help your peers!
Select
Add answer anonymously...

ShareChat Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Merge Sort Problem Statement You are given a sequence of numbers, ARR. Your task...read more
A Software Developer Intern was asked Q. Flood Fill Algorithm Task Assist Ninja in altering the color of a specific regio...read more
A Software Developer Intern was asked Q. Saving Money Problem Statement Ninja is adventurous and loves traveling while be...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Merge Sort Problem Statement You are given a sequence of numbers, ARR. Your task...read more
A Software Developer Intern was asked Q2. Flood Fill Algorithm Task Assist Ninja in altering the color of a specific regio...read more
A Software Developer Intern was asked Q3. Saving Money Problem Statement Ninja is adventurous and loves traveling while be...read more
ShareChat 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