09 Sept 2026Go / Python / TypeScriptEasy

Flipping an Image

Horizontally reverse each binary image row and invert every bit.

Build each output row by reading the source row backward and XORing every bit with one.

complexity

O(rows * columns) time and O(rows * columns) output space.

solution files

  • Go flipping-an-image/solution.go
  • Python flipping-an-image/solution.py
  • TypeScript flipping-an-image/solution.ts

Solution files

Goflipping-an-image/solution.go
package main

func flipAndInvertImage(image [][]int) [][]int {
	result := make([][]int, len(image))
	for rowIndex, row := range image {
		result[rowIndex] = make([]int, len(row))
		for index, bit := range row {
			result[rowIndex][len(row)-1-index] = bit ^ 1
		}
	}
	return result
}
Pythonflipping-an-image/solution.py
class Solution:
    def flipAndInvertImage(self, image: list[list[int]]) -> list[list[int]]:
        return [[bit ^ class="syntax-number">1 for bit in reversed(row)] for row in image]
TypeScriptflipping-an-image/solution.ts
function flipAndInvertImage(image: number[][]): number[][] {
  return image.map((row) => [...row].reverse().map((bit) => bit ^ class="syntax-number">1));
}