Flipping an Image

Easy · Matrix · Array · Two Pointers · Bit Manipulation · Matrix · Simulation

Problem

You are given an n x n binary matrix image. Transform it in two steps and return the result:

You may change image in place and return it, or return a new matrix.

Examples

Input: image = [[1,0,0],[1,1,0],[0,1,0]]
Output: [[1,1,0],[1,0,0],[1,0,1]]

Flipping gives [[0,0,1],[0,1,1],[0,1,0]], and inverting that gives the output.

Input: image = [[0,1],[1,1]]
Output: [[0,1],[0,0]]

The flipped rows are [1,0] and [1,1], which invert to [0,1] and [0,0].

Input: image = [[1]]
Output: [[0]]

Flipping one cell changes nothing; inverting turns 1 into 0.

Constraints

Approaches

Follow-up

Notice that when row[j] != row[n-1-j], flipping swaps two different bits and inverting swaps them back: that pair ends up unchanged. Only pairs of equal bits change, and both flip. So the whole job is: for each mirrored pair of equal bits, invert both; leave unequal pairs alone.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…