Skip to main content
Back to problems
Leetcode
Medium
Arrays
Matrices
Bit Manipulation
Flipping an Image

Flip a binary matrix horizontally and then invert every bit.

Acceptance 100%
Problem Statement

Problem

You are given an n×nn \times n binary matrix image where each element is either 0 or 1.

Perform two operations in order:

  1. Flip horizontally each row, so the row is reversed from left to right.
  2. Invert the matrix, changing every 0 to 1 and every 1 to 0.

Return the transformed matrix.

Notes

  • Flipping horizontally means reversing each row independently.
  • Inverting means replacing each bit with its opposite value.

Input Format

  • An integer matrix image of size n×nn \times n
  • image[i][j] is either 0 or 1

Output Format

  • Return the matrix after horizontally flipping each row and then inverting every value.

Constraints

  • 1n201 \le n \le 20
  • image is a square matrix
  • Each entry is either 0 or 1
Examples
Sample cases returned by the problem API.

Example 1

Input

image = [[1,1,0],[1,0,1],[0,0,0]]

Output

[[1,0,0],[0,1,0],[1,1,1]]

Explanation

Reverse each row first: [[0,1,1],[1,0,1],[0,0,0]]. Then invert every bit: [[1,0,0],[0,1,0],[1,1,1]].

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.