Remove all zero-rows and all zero-columns from a Matrix
Given a matrix arr[][] of size N * M, the task is to print the matrix after removing all rows and columns from the matrix which consists of 0s only.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: arr[][] ={ { 1, 1, 0, 1 }, { 0, 0, 0, 0 }, { 1, 1, 0, 1}, { 0, 1, 0, 1 } }
Output:
111
111
011
Explanation:
Initially, the matrix is as follows:
arr[][] = { { 1, 1, 0, 1 },
{ 0, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } }
Removing the 2nd row modifies the matrix to:
arr[][] = { { 1, 1, 0, 1 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } }
Removing the 3rd column modifies the matrix to:
arr[][] = { { 1, 1, 1 },
{ 1, 1, 1 },
{ 0, 1, 1 } }Input: arr={{0, 1}, {0, 1}}
Output:
1
1
Approach: The idea is to count the number of 0s in all the rows and columns of the matrix and check if any rows or columns consist only of 0s or not. If found to be true, then remove those rows or the columns of the matrix. Follow the steps below to solve the problem:
- Traverse the matrix and count 1s in rows and columns.
- Now, traverse over the loop again and check for the following:
- If the count of 1s is found to be 0 for any row, skip that row.
- If the count of 1s is found to be greater than 0 for any column, print that element.
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to remove the rows or columns from // the matrix which contains all 0s elements void removeZeroRowCol(vector<vector<int> >& arr) { // Stores count of rows int n = arr.size(); // col[i]: Stores count of 0s // in current column int col[n + 1] = { 0 }; // row[i]: Stores count of 0s // in current row int row[n + 1] = { 0 }; // Traverse the matrix for (int i = 0; i < n; ++i) { // Stores count of 0s // in current row int count = 0; for (int j = 0; j < n; ++j) { // Update col[j] col[j] += (arr[i][j] == 1); // Update count count += (arr[i][j] == 1); } // Update row[i] row[i] = count; } // Traverse the matrix for (int i = 0; i < n; ++i) { // If all elements of // current row is 0 if (row[i] == 0) { continue; } for (int j = 0; j < n; ++j) { // If all elements of // current column is 0 if (col[j] != 0) cout << arr[i][j]; } cout << "\n"; } } // Driver Code int main() { vector<vector<int> > arr = { { 1, 1, 0, 1 }, { 0, 0, 0, 0 }, { 1, 1, 0, 1 }, { 0, 1, 0, 1 } }; // Function Call removeZeroRowCol(arr); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to remove the rows or columns from // the matrix which contains all 0s elements static void removeZeroRowCol(int arr[][]) { // Stores count of rows int n = arr.length; // col[i]: Stores count of 0s // in current column int col[] = new int[n + 1]; // row[i]: Stores count of 0s // in current row int row[] = new int[n + 1]; // Traverse the matrix for(int i = 0; i < n; ++i) { // Stores count of 0s // in current row int count = 0; for(int j = 0; j < n; ++j) { if (arr[i][j] == 1) // Update col[j] col[j] += 1; else col[j] += 0; if (arr[i][j] == 1) // Update count count += 1; else count += 0; } // Update row[i] row[i] = count; } // Traverse the matrix for(int i = 0; i < n; ++i) { // If all elements of // current row is 0 if (row[i] == 0) { continue; } for(int j = 0; j < n; ++j) { // If all elements of // current column is 0 if (col[j] != 0) System.out.print(arr[i][j]); } System.out.println(); } } // Driver Code public static void main (String[] args) { int arr[][] = { { 1, 1, 0, 1 }, { 0, 0, 0, 0 }, { 1, 1, 0, 1 }, { 0, 1, 0, 1 } }; // Function Call removeZeroRowCol(arr); }}// This code is contributed by AnkThon |
Python3
# Python3 program for the above approach # Function to remove the rows or columns from # the matrix which contains all 0s elements def removeZeroRowCol(arr) : # Stores count of rows n = len(arr) # col[i]: Stores count of 0s # in current column col = [0] * (n + 1) # row[i]: Stores count of 0s # in current row row = [0] * (n + 1) # Traverse the matrix for i in range(n) : # Stores count of 0s # in current row count = 0 for j in range(n) : # Update col[j] col[j] += (arr[i][j] == 1) # Update count count += (arr[i][j] == 1) # Update row[i] row[i] = count # Traverse the matrix for i in range(n) : # If all elements of # current row is 0 if (row[i] == 0) : continue for j in range(n) : # If all elements of # current column is 0 if (col[j] != 0) : print(arr[i][j], end = "") print() arr = [ [ 1, 1, 0, 1 ], [ 0, 0, 0, 0 ], [ 1, 1, 0, 1 ], [ 0, 1, 0, 1 ] ] # Function Call removeZeroRowCol(arr)# This code is contributed by divyeshrabadiya07 |
C#
// C# program for the above approach using System;class GFG{ // Function to remove the rows or columns from // the matrix which contains all 0s elements static void removeZeroRowCol(int[,] arr) { // Stores count of rows int n = arr.GetLength(0); // col[i]: Stores count of 0s // in current column int[] col = new int[n + 1]; // row[i]: Stores count of 0s // in current row int[] row = new int[n + 1]; // Traverse the matrix for(int i = 0; i < n ; ++i) { // Stores count of 0s // in current row int count = 0; for(int j = 0; j < n ; ++j) { if (arr[i, j] == 1) // Update col[j] col[j] += 1; else col[j] += 0; if (arr[i, j] == 1) // Update count count += 1; else count += 0; } // Update row[i] row[i] = count; } // Traverse the matrix for(int i = 0; i < n; ++i) { // If all elements of // current row is 0 if (row[i] == 0) { continue; } for(int j = 0; j < n; ++j) { // If all elements of // current column is 0 if (col[j] != 0) Console.Write(arr[i, j]); } Console.WriteLine(); } } // Driver Code public static void Main (String[] args) { int[,] arr = { { 1, 1, 0, 1 }, { 0, 0, 0, 0 }, { 1, 1, 0, 1 }, { 0, 1, 0, 1 } }; // Function Call removeZeroRowCol(arr); }}// This code is contributed by susmitakundugoaldanga |
Javascript
<script>// Javascript program to implement// the above approach// Function to remove the rows or columns from // the matrix which contains all 0s elements function removeZeroRowCol(arr) { // Stores count of rows let n = arr.length; // col[i]: Stores count of 0s // in current column let col = Array.from({length: n+1}, (_, i) => 0); // row[i]: Stores count of 0s // in current row let row = Array.from({length: n+1}, (_, i) => 0); // Traverse the matrix for(let i = 0; i < n; ++i) { // Stores count of 0s // in current row let count = 0; for(let j = 0; j < n; ++j) { if (arr[i][j] == 1) // Update col[j] col[j] += 1; else col[j] += 0; if (arr[i][j] == 1) // Update count count += 1; else count += 0; } // Update row[i] row[i] = count; } // Traverse the matrix for(let i = 0; i < n; ++i) { // If all elements of // current row is 0 if (row[i] == 0) { continue; } for(let j = 0; j < n; ++j) { // If all elements of // current column is 0 if (col[j] != 0) document.write(arr[i][j]); } document.write("<br/>"); } } // Driver Code let arr = [[ 1, 1, 0, 1 ], [ 0, 0, 0, 0 ], [ 1, 1, 0, 1 ], [ 0, 1, 0, 1 ]]; // Function Call removeZeroRowCol(arr); // This code is contributed by souravghosh0416.</script> |
Output :
111 111 011
Time complexity: O(
)
Space Complexity: O(
)


