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.
We'll cover the following...
Statement
Given that an image is represented by an matrix containing s and s, 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 horizontally results in .
Inverting an image means that every is replaced by , and every is replaced by . Inverting results in .
Constraints:
- Image should be a square matrix.
images[i][j]is either or .
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
Consider the following matrix as input. What is the output once it has been flipped and inverted?
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.
Try it yourself
Implement your solution in the following coding playground.
import java.util.*;public class FlipImage{public static int[][] flipAndInvertImage(int[][] image) {// Replace this placeholder return statement with your codereturn new int[][]{};}}