Flipping an Image
Explore how to flip a binary image horizontally and invert it by transforming 0s to 1s and vice versa. Understand constraints and implement a solution using bitwise manipulation techniques. This lesson helps you solve image transformation problems efficiently, a useful skill in coding interviews.
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
Understand 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.
package mainfunc flipAndInvertImage(image [][]int) [][]int {// Replace this placeholder return statement with your codereturn image}