Problem · Matrix
Transform Binary Matrix
Learn this problemProblem statement
You are given an n x n binary matrix representing an image.
Your task is to transform the image by applying the following operations in order:
- Rotate the image clockwise by
rotationdegrees.rotationis one of90,180, or270.
- If
vertical_flip = 1, flip the image vertically, top to bottom. - If
horizontal_flip = 1, flip the image horizontally, left to right.
Return the final transformed binary matrix after all operations are applied.
Function
transformImage(n: int, rotation: int, vertical_flip: int, horizontal_flip: int, image: int[][]) → int[][]Examples
Example 1
n = 3rotation = 270vertical_flip = 0horizontal_flip = 1image = [[1,0,0],[0,1,1],[0,0,1]]return = [[1,1,0],[0,1,0],[0,0,1]]Original pixels:
[[1,0,0],
[0,1,1],
[0,0,1]]Rotate the image by 270 degrees clockwise:
[[0,1,1],
[0,1,0],
[1,0,0]]vertical_flip is 0, so the image is not flipped vertically.
Perform the horizontal flip:
[[1,1,0],
[0,1,0],
[0,0,1]]Note: The screenshot did not show a separate output line. FastPrep calculated this matrix from the visible rotation and flip steps so you can practice with a complete example. 🐥