✅ Beats 100 % || Time O(n * m) || Space O(1) || Java - 0ms || Easy Explained ✅
\n\n\n# Bit Manupulation\n\nThank you for catching that oversight. Let me correct that.\n\n# Intuition\nThe problem asks to flip an image horizontally and inver
\n![image.png](https://assets.leetcode.com/users/images/66e37527-e193-48ba-be5a-aefe90525329_1712326772.6553497.png)\n\n# Bit Manupulation\n\nThank you for catching that oversight. Let me correct that.\n\n# Intuition\nThe problem asks to flip an image horizontally and invert it. Flipping horizontally means reversing each row of the image, and inverting means changing 0s to 1s and 1s to 0s.\n\n# Approach\n1. Iterate through each row of the image array.\n2. For each row, initialize two pointers `j` and `k` pointing to the start and end of the row, respectively.\n3. While `j` is less than or equal to `k`, swap the elements at indices `j` and `k` of the current row and invert them using the XOR (^) operation with 1.\n4. Increment `j` and decrement `k` to move towards the center of the row.\n5. Repeat steps 3-4 until `j` becomes greater than `k`, indicating that the entire row has been processed.\n6. After processing all rows, return the modified image array.\n\n# Complexity\n- Time complexity:\n  - The algorithm iterates through each element of the image array once, performing constant time operations on each element, resulting in a time complexity of O(m * n), where m is the number of rows and n is the number of columns in the image.\n- Space complexity:\n  - The algorithm modifies the input image array in place, without using any additional space, resulting in a space complexity of O(1).\n\n# Code\n```java []\nclass Solution {\n    public int[][] flipAndInvertImage(int[][] image) {\n        // Iterate through each row of the image array\n        for (int i = 0; i < image.length; i++) {\n            int j = 0, k = image[i].length - 1; // Initialize pointers for flipping\n            // Flip and invert elements until j <= k\n            while (j <= k) {\n                int tmp = image[i][j]; // Store element at index j\n                // Invert and swap elements at indices j and k\n                image[i][j] = image[i][k] ^ 1;\n                image[i][k] = tmp ^ 1;\n                j++; // Move j towards center\n                k--; // Move k towards center\n            }\n        }\n        return image; // Return the modified image array\n    }\n}\n```\n```java []\nclass Solution {\n    public int[][] flipAndInvertImage(int[][] image) {\n        int rows = image.length;\n\n        // Iterate through each row of the image\n        for (int i = 0; i < rows; i++) {\n            int j = 0, k = image[i].length - 1;\n            // Flip and invert each element in the row\n            while (j <= k) {\n                // Swap elements and invert them\n                int tmp = image[i][j];\n                image[i][j] = image[i][k] == 0 ? 1 : 0;\n                image[i][k] = tmp == 0 ? 1 : 0;\n                j++;\n                k--;\n            }\n        }\n\n        return image; // Return the modified image\n    }\n}\n```\n
['Array', 'Two Pointers', 'Bit Manipulation', 'Matrix', 'Simulation', 'Java']