Java | TC: O(N^2) | SC: O(1) | Optimal one-pass in-place solution
java\n/**\n * Optimal one-pass in-place solution\n *\n * If the values are not same, swap and flip will not change anything.\n * If the values are same, we will
```java\n/**\n * Optimal one-pass in-place solution\n *\n * If the values are not same, swap and flip will not change anything.\n * If the values are same, we will flip both.\n *\n * Time Complexity: O(N^2)\n *\n * Space Complexity: O(1)\n *\n * N = Matrix Size\n */\nclass Solution {\n    public int[][] flipAndInvertImage(int[][] image) {\n        if (image == null || image.length == 0 || image[0].length == 0) {\n            return image;\n        }\n\n        for (int[] row : image) {\n            int start = 0;\n            int end = row.length - 1;\n            while (start <= end) {\n                if (row[start] == row[end]) {\n                    row[start] ^= 1;\n                    row[end] = row[start];\n                }\n                start++;\n                end--;\n            }\n        }\n\n        return image;\n    }\n}\n```
['Array', 'Two Pointers', 'Matrix', 'Java']