Search⌘ K
AI Features

Flipping an Image

Understand how to manipulate a square binary matrix by flipping it horizontally and then inverting each bit. This lesson guides you through implementing a solution that reflects the image horizontally and replaces all 0s with 1s and vice versa, helping you master bitwise problem-solving techniques.

Statement

Given that an image is represented by an (n×n)(n \times n) matrix containing 00s and 11s, flip and invert the image, and return the resultant image.

Horizontally flipping an image means that the mirror image of the matrix should be returned. Flipping [1,0,0][1, 0, 0] horizontally results in [0,0,1][0, 0, 1].

Inverting an image means that every 00 is replaced by 11, and every 11 is replaced by 00. Inverting [0,1,1][0, 1, 1] results in [1,0,0][1, 0, 0].

Constraints:

  • Image should be a square matrix.
  • 1n201 \leq n \leq 20
  • images[i][j] is either 00 or 11.

Examples

Understanding the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Flipping an Image

1.

Consider the following (3×3)(3 \times 3) matrix as input. What is the output once it has been flipped and inverted?

[110001011]\begin{bmatrix} 1 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix}

A.

[110001011]\begin{bmatrix} 1 & 1 & 0 \\ 0 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix}

B.

[100011001] \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \end{bmatrix}

C.

[110101011] \begin{bmatrix} 1 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix}


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

Java
usercode > FlipImage.java
import java.util.*;
public class FlipImage{
public static int[][] flipAndInvertImage(int[][] image) {
// Replace this placeholder return statement with your code
return new int[][]{};
}
}
Flipping an Image