Flip and Invert Image
Mirror each row of a 0/1 image and swap its 0s and 1s, both in one pass over the row.
Given an n x nimage[1, 1, 0][0, 1, 1][0, 1, 1][1, 0, 0]
Example 1
Output: [[1,0,0],[0,1,0],[1,1,1]]
Example 2
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
The Idea
Imagine a tiny black-and-white image made of squares, where 1 is a black square and 0 a white one. Flipping holds the image up to a mirror, so each row now reads from right to left. Inverting turns it into a photo negative: black squares become white and white squares become black.
Look at the top half of the picture below, which takes Example 1 through both steps. Row 0 of the grid labelled "image" is 1, 1, 0. In the grid "after flipping each row" it reads 0, 1, 1, and in the grid "after inverting every cell" it reads 1, 0, 0, which is row 0 of the expected output.
Done that way, every row is walked twice, once to reverse it and once to invert it. Could one walk do both? Look at where each value in the last grid came from. The 1 in row 0, col 0 is the 0 that sat in row 0, col 2 of the image, inverted. So every cell of the answer is the cell at the mirror position in the same row, inverted.
Pairing each cell with its mirror suggests walking in from both ends of a row at once. Keep one marker, ij
The bottom half of the picture does this for row 1, which is 1, 0, 1. In the box labelled "start", ij
Pairing the leftmost and rightmost cells that are still open, and moving the two markers inward until they meet, is often called the two-pointer method; here the two pointers are just the column numbers ij
Solution Approach
Turning that idea into code means settling three things: inverting one cell, handling a pair of cells from the two ends, and the middle cell of a row with an odd number of cells. There is also an optional shortcut that leaves some pairs alone.
Inverting one cell
How do we turn a 0 into 1 and a 1 into 0? Writing 1 - x^
A cell is a Java int000...00010 ^ 1 = 11 ^ 1 = 0
Swapping and inverting a pair
Why not reverse the row first and then invert it? That also gives the right answer in the same order of time, but it walks the row twice. The pair step does both at once: save the left cell, inverted, in a variable named leftleft
The variable left
The middle cell
What happens when a row has an odd number of cells, like the rows of Example 1? After the outer pairs are done, iji <= ji == jleft
For an even number of cells, as in Example 2, the markers pass each other without meeting, and every cell belongs to a pair.
Skipping pairs that differ
Take a pair that differs, such as 1 on the left and 0 on the right. Swapping gives 0 and 1, and inverting gives 1 and 0 again, so the pair ends up as it started. A pair with equal values does change: 1 and 1 become 0 and 0, and 0 and 0 become 1 and 1.
As an optional improvement, the loop can invert both cells only when row[i] == row[j]
Java Solution
The method flipAndInvertImage
- A for-each loop takes the rows of
one at a time, asimage . The cells are changed in place, so no new matrix is made.row - For each row, set
to 0, the first column, andi toj , the last column.row.length - 1 - Inside, a
loop runs as long aswhile . It savesi <= j inrow[i] ^ 1 , writesleft intorow[j] ^ 1 , and writesrow[i] intoleft . Thenrow[j] moves one column right andi one column left.j - When every row is done, the method returns
, which now holds the answer.image
The mainshow
import java.util.Arrays;
public class FlipAndInvertImage {
public static int[][] flipAndInvertImage(int[][] image) {
for (int[] row : image) {
int i = 0;
int j = row.length - 1;
while (i <= j) {
int left = row[i] ^ 1; // the left value, inverted
row[i] = row[j] ^ 1; // the right value, inverted, moves left
row[j] = left; // the left value, inverted, moves right
i++;
j--;
}
}
return image;
}
static void show(String label, int[][] image) {
System.out.println(label);
for (int[] row : image) {
System.out.println(" " + Arrays.toString(row));
}
}
public static void main(String[] args) {
show("Example 1:", flipAndInvertImage(new int[][] {{1, 1, 0}, {1, 0, 1}, {0, 0, 0}}));
show("Example 2:", flipAndInvertImage(new int[][] {
{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}}));
show("One cell, 0:", flipAndInvertImage(new int[][] {{0}}));
show("One cell, 1:", flipAndInvertImage(new int[][] {{1}}));
show("Mixed pairs:", flipAndInvertImage(new int[][] {{1, 0}, {0, 1}}));
}
}
Running it prints:
Example 1:
[1, 0, 0]
[0, 1, 0]
[1, 1, 1]
Example 2:
[1, 1, 0, 0]
[0, 1, 1, 0]
[0, 0, 0, 1]
[1, 0, 1, 0]
One cell, 0:
[1]
One cell, 1:
[0]
Mixed pairs:
[1, 0]
[0, 1]
Under "Example 1:", the middle line [0, 1, 0]
The single cells come out as [1][0]iji <= j[1, 0][0, 1]
That <=while (i < j)[[1, 1, 0], [0, 0, 0], [1, 0, 1]]ij
Complexity
Each cell is handled in one step of the loop, and the work needs no storage beyond three numbers.
- Time: O(n2), because the image has n rows of n cells, and in each row the markers cover all n cells in about n / 2 steps of two cells each.
- Space: O(1), because the image is changed in place and each row needs only
,i andj .left
Markers that move in from both ends of an array come up in many other problems; see