Flipping an Image
Explore how to flip an n by n binary image horizontally and invert its pixels by replacing zeros with ones and vice versa. Understand the key bitwise operations that enable efficient manipulation of image data in matrix form. Practice solving this problem step-by-step to strengthen your approach to similar coding interview questions using C++.
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:
#include <iostream>vector<vector<int>> FlipAndInvertImage(vector<vector<int>> image){// Replace this placeholder return statement with your codereturn image;}