Given a binary matrix. The task is to flip the matrix horizontally(find the image of the matrix), then invert it.

**Note**:

- To
**flip**a matrix horizontally means that reversing each row of the matrix. For example, flipping [1, 1, 0, 0] horizontally results in [0, 0, 1, 1]. - To
**invert**a matrix means that replacing each 0 by 1 and vice-versa. For example, inverting [0, 0, 1] results in [1, 1, 0].

**Examples**:

Input: mat[][] = [[1, 1, 0], [1, 0, 1], [0, 0, 0]]Output: [[1, 0, 0], [0, 1, 0], [1, 1, 1]]Explanation: First reverse each row: [[0, 1, 1], [1, 0, 1], [0, 0, 0]] Then, invert the image: [[1, 0, 0], [0, 1, 0], [1, 1, 1]]Input: mat[][] = [[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]Output: [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]]Explanation: First reverse each row: [[0, 0, 1, 1], [1, 0, 0, 1], [1, 1, 1, 0], [0, 1, 0, 1]]. Then invert the image: [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]]

**Approach:** We can do this in-place. On observing carefully, it can be deduced that in each row in the final matrix, the i-th value from the left is equal to the inverse of the i-th value from the right of the input binary matrix.

Thus, we use (Column + 1) / 2 (with floor division) to iterate over all indexes in the first half of the row, including the centre and updating the answer accordingly.

Below is the implementation of above approach:

`# Python3 implementation of above approach ` `# Function to return final Image ` ` ` `def` `fliped_Invert_Image(mat): ` ` ` ` ` `for` `row ` `in` `mat: ` ` ` `for` `i ` `in` `range` `((` `len` `(row) ` `+` `1` `) ` `/` `/` `2` `): ` ` ` ` ` `row[i] ` `=` `row[` `len` `(row) ` `-` `1` `-` `i] ^ ` `1` ` ` `row[` `len` `(row) ` `-` `1` `-` `i] ` `=` `row[i] ^ ` `1` ` ` ` ` `# return Flipped and Inverted image ` ` ` `return` `mat ` ` ` `# Driver code ` `mat ` `=` `[[` `1` `, ` `1` `, ` `0` `, ` `0` `], [` `1` `, ` `0` `, ` `0` `, ` `1` `], [` `0` `, ` `1` `, ` `1` `, ` `1` `], [` `1` `, ` `0` `, ` `1` `, ` `0` `]] ` ` ` `print` `(fliped_Invert_Image(mat)) ` |

*chevron_right*

*filter_none*

**Output:**

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

**Time Complexity:** O(N*M), where N is the number of rows and M is the number of columns in the given binary matrix.

## Recommended Posts:

- Minimum flip required to make Binary Matrix symmetric
- Find longest sequence of 1's in binary representation with one flip
- Count minimum bits to flip such that XOR of A and B equal to C
- Maximum score after flipping a Binary Matrix atmost K times
- Program to check diagonal matrix and scalar matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Meta Binary Search | One-Sided Binary Search
- Gray to Binary and Binary to Gray conversion
- Determinant of a Matrix
- Matrix Exponentiation
- Centrosymmetric Matrix
- Pascal Matrix
- Bisymmetric matrix
- Hilbert Matrix
- Adjoint and Inverse of a Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.