Horizontally Flip a Binary Matrix - GeeksforGeeks

18 Apr, 2022
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
# Python3 implementation of above approach# Function to return final Image def flipped_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 codemat = [[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]] print(flipped_Invert_Image(mat))
[[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. 
C++14
Python3
//c++ program to flip the binary image horizontally#include<bits/stdc++.h>using namespace std;//function to flip the matrix using two pointer techniquevector<vector<int>> flip_matrix(vector<vector<int>>matrix){    for(int i=0;i<matrix.size();i++)         {              int left = 0;             int right = matrix[i].size()-1;                           while( left <= right )             {                 //conditions executes if compared elements are not equal                 if(matrix[i][left] == matrix[i][right])                 {                     // if element is 0 it becomes 1 if not it becomes 0                     matrix[i][left] = 1-matrix[i][left];                     matrix[i][right] = matrix[i][left];                 }                 left++;                 right--;             }         }         //return matrix        return matrix;}//Drive codeint main(){    vector<vector<int>>matrix={{1,1,0},{1,0,1},{0,0,0}};    vector<vector<int>>v=flip_matrix(matrix);    for(int i=0;i<matrix.size();i++)    {        for(int j=0;j<matrix[i].size();j++)        {            cout<<v[i][j]<<" ";        }        cout<<'\n';    }}
# Python program to flip the binary image horizontally # function to flip the matrix using two pointer techniquedef flip_matrix(matrix):     for i in range(len(matrix)):        left,right = 0,len(matrix[i])-1                     while(left <= right):                       # conditions executes if compared elements are not equal            if(matrix[i][left] == matrix[i][right]):                               #  if element is 0 it becomes 1 if not it becomes 0                matrix[i][left] = 1-matrix[i][left]                matrix[i][right] = matrix[i][left]                             left += 1            right -= 1             # return matrix    return matrix # Drive codematrix = [[1, 1, 0], [1, 0, 1], [0, 0, 0]]v = flip_matrix(matrix) for i in range(len(matrix)):    for j in range(len(matrix[i])):       print(v[i][j],end = " ")    print() # This code is contributed by shinjanpatra
surinderdawra388
bunny09262002
inversion
Backtracking
Bit Magic
Mathematical
Mathematical
Bit Magic
Backtracking
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
The Knight's tour problem | Backtracking-1
Backtracking | Introduction
m Coloring Problem | Backtracking-5
Hamiltonian Cycle | Backtracking-6
Print all paths from a given source to a destination
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?