Solution: Flipping an Image
Let's solve the Flipping an Image problem using the Bitwise Manipulation pattern.
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 .
Solution
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach
The naive approach of solving this problem is to create separate functions for flipping the matrix horizontally and then inverting each individual element of the matrix. We’ll use nested loops to flip the rows of the matrix by swapping the elements from both ends until we reach the middle of the row. Once the matrix has been flipped, we’ll traverse it again and replace s with s and s with s to invert it. The naive approach uses two iterations of the whole matrix to flip and invert the matrix.
The time complexity of flipping the matrix is . The time complexity for inverting each element is also .
Optimized approach using bitwise manipulation
The problem statement asks us to flip and invert the binary s and s. This can be done by performing bitwise XOR on each row of the matrix. Problems such as this one are good examples to solve using the bitwise manipulation pattern. We will flip and invert the matrix using one iteration of the matrix.
While the overall time complexity of this approach is the same as the naive approach discussed above, bitwise operations have hardware support and, hence, are relatively faster compared to conditional statements. Instead of adding if
statements to invert the elements, we can simply compute their XOR with 1
. When a 0
is encountered, it gets replaced with 1
as 0 XOR 1 = 1
and when a 1
is encountered, it’s replaced with 0
as 1 XOR 1 = 0
.
Here is how we implement this algorithm:
-
Compute the index of the middle element in each row.
-
For each element in the first half of each row, we will perform the following two operations:
- We will compute the bitwise XOR of the current element with 1, which will invert the element’s value. If the value is 0, it will be replaced with 1, and if the value is 1, it will be replaced with 0.
- We will then swap the current element with the corresponding last element in the second half of the row, i.e., the element at the same distance from the end of the row as the current element is from the beginning of the row, after performing the same XOR operation on the second element as well.
-
Return the modified array after iterating through all the rows and flipping/inverting each element.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.