KatInterview

861. Flipping an Image

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method Explanation

The problem requires us to perform two operations on each row of a given n x n binary matrix: first, flip it horizontally, and then invert it. A horizontal flip reverses the order of elements in a row. An inversion replaces every 0 with a 1 and every 1 with a 0.

Solution

Flipping an Image in Python

class Solution:
    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
        n = len(image)  # The matrix is n x n

        # Process each row of the image.
        for row in image:
            left, right = 0, n - 1

            # Use two pointers to swap and invert elements from the outside in.
            while left <= right:
                # Python's tuple assignment allows a clean swap. The right-hand side is fully
                # evaluated before assignment, so we use the original values of row[left]
                # and row[right] to calculate their new, inverted-and-swapped values.
                # XOR (^) is used to invert the bits (0->1, 1->0).
                row[left], row[right] = row[right] ^ 1, row[left] ^ 1
                
                # Move the pointers towards the center.
                left += 1
                right -= 1
        
        return image

Flipping an Image in Java

class Solution {
    public int[][] flipAndInvertImage(int[][] image) {
        int n = image.length; // The matrix is n x n

        // Process each row of the image.
        for (int[] row : image) {
            int left = 0;
            int right = n - 1;

            // Use two pointers to swap and invert elements from the outside in.
            while (left <= right) {
                // This logic combines horizontal flip and inversion in one step.
                // The new value at `left` should be the inverted original value at `right`.
                // The new value at `right` should be the inverted original value at `left`.
                // We use a temporary variable to hold the original value of `row[left]`.
                // Using XOR (^) is a concise way to invert 0s and 1s.
                int temp = row[left];
                row[left] = row[right] ^ 1;
                row[right] = temp ^ 1;
                
                // Move the pointers towards the center.
                left++;
                right--;
            }
        }

        return image;
    }
}

Flipping an Image in C++

class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& image) {
        int n = image.size(); // The matrix is n x n

        // Process each row of the image.
        for (auto& row : image) {
            int left = 0;
            int right = n - 1;

            // Use two pointers to swap and invert elements from the outside in.
            while (left <= right) {
                // This logic combines horizontal flip and inversion in one step.
                // The new value at `left` should be the inverted original value at `right`.
                // The new value at `right` should be the inverted original value at `left`.
                // We use a temporary variable to hold the original value of `row[left]`.
                int temp = row[left];
                row[left] = 1 - row[right]; // or row[right] ^ 1
                row[right] = 1 - temp;      // or temp ^ 1
                
                // Move the pointers towards the center.
                left++;
                right--;
            }
        }

        return image;
    }
};

Flipping an Image in TypeScript

function flipAndInvertImage(image: number[][]): number[][] {
    const n = image.length; // The matrix is n x n

    // Process each row of the image.
    for (const row of image) {
        let left = 0;
        let right = n - 1;

        // Use two pointers to swap and invert elements from the outside in.
        while (left <= right) {
            // TypeScript's destructuring assignment works like Python's tuple unpacking.
            // The right-hand side array is created first, using original values, before
            // assignment to the left-hand side variables.
            // XOR (^) is used to invert the bits (0->1, 1->0).
            [row[left], row[right]] = [row[right] ^ 1, row[left] ^ 1];

            // Move the pointers towards the center.
            left++;
            right--;
        }
    }

    return image;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.