Given a matrix with N rows and M columns. The task is to print the index of columns of the given matrix based on the increasing number of zeroes in each column.

For Example, If the 1st column contains 2 zero’s, the 2nd column contains 1 zero, and 3rd column does not contain any zeroes. Then the output will be 3, 2, 1.

**Note**: The matrix is considered to be having 1-based indexing.

**Examples: **

Input: mat[N][M] ={{0, 0, 0}, {0, 2, 0}, {0, 1, 1}, {1, 1, 1}};Output: 2 3 1 No. of zeroes in first col: 3 No. of zeroes in second col: 1 No of zeroes in third col: 2 Therefore, sorted order of count is 1 2 3 and their corresponding column numbers are, 2 3 1Input: mat[N][M] ={{0, 0, 0}, {0, 0, 3}, {0, 1, 1}};Output: 3 2 1

**Approach**:

- Create a Vector of pair to store count of zeroes in each column. Where the first element of the pair is count and the second element of the pair is corresponding column index.
- Traverse the matrix column wise.
- Insert the count of zeroes for each column in the vector of pair.
- Sort the vector of pair according to the count of zeroes.
- Print the second element of the pair which contains indexes of the columns sorted according to the count of zeroes.

Below is the implementation of the above approach:

## C++

`// C++ Program to print the index of columns` `// of the given matrix based on the` `// increasing number of zeroes in each column` `#include <bits/stdc++.h>` `using` `namespace` `std;` `#define N 4 // rows` `#define M 3 // columns` `// Function to print the index of columns` `// of the given matrix based on the` `// increasing number of zeroes in each column` `void` `printColumnSorted(` `int` `mat[N][M])` `{` ` ` `// Vector of pair to store count of zeroes` ` ` `// in each column.` ` ` `// First element of pair is count` ` ` `// Second element of pair is column index` ` ` `vector<pair<` `int` `, ` `int` `> > colZeroCount;` ` ` `// Traverse the matrix column wise` ` ` `for` `(` `int` `i = 0; i < M; i++) {` ` ` `int` `count = 0;` ` ` `for` `(` `int` `j = 0; j < N; j++) {` ` ` `if` `(mat[j][i] == 0)` ` ` `count++;` ` ` `}` ` ` `// Insert the count of zeroes for each column` ` ` `// in the vector of pair` ` ` `colZeroCount.push_back(make_pair(count, i));` ` ` `}` ` ` `// Sort the vector of pair according to the` ` ` `// count of zeroes` ` ` `sort(colZeroCount.begin(), colZeroCount.end());` ` ` `// Print the second element of the pair which` ` ` `// contain indexes of the sorted vector of pair` ` ` `for` `(` `int` `i = 0; i < M; i++)` ` ` `cout << colZeroCount[i].second + 1 << ` `" "` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `mat[N][M] = { { 0, 0, 0 },` ` ` `{ 0, 2, 0 },` ` ` `{ 0, 1, 1 },` ` ` `{ 1, 1, 1 } };` ` ` `printColumnSorted(mat);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print the index of ` `# columns of the given matrix based ` `# on the increasing number of zeroes ` `# in each column` `# Rows` `N ` `=` `4` `# Columns` `M ` `=` `3` `# Function to print the index of columns` `# of the given matrix based on the` `# increasing number of zeroes in ` `# each column` `def` `printColumnSorted(mat):` ` ` ` ` `# Vector of pair to store count ` ` ` `# of zeroes in each column.` ` ` `# First element of pair is count` ` ` `# Second element of pair is column index` ` ` `colZeroCount ` `=` `[]` ` ` `# Traverse the matrix column wise` ` ` `for` `i ` `in` `range` `(M):` ` ` `count ` `=` `0` ` ` `for` `j ` `in` `range` `(N):` ` ` `if` `(mat[j][i] ` `=` `=` `0` `):` ` ` `count ` `+` `=` `1` ` ` `# Insert the count of zeroes for` ` ` `# each column in the vector of pair` ` ` `colZeroCount.append((count, i))` ` ` `# Sort the vector of pair according ` ` ` `# to the count of zeroes` ` ` `colZeroCount ` `=` `sorted` `(colZeroCount)` ` ` `# Print the second element of the ` ` ` `# pair which contain indexes of the` ` ` `# sorted vector of pair` ` ` `for` `i ` `in` `range` `(M):` ` ` `print` `(colZeroCount[i][` `1` `] ` `+` `1` `, end ` `=` `" "` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `mat ` `=` `[ [ ` `0` `, ` `0` `, ` `0` `],` ` ` `[ ` `0` `, ` `2` `, ` `0` `],` ` ` `[ ` `0` `, ` `1` `, ` `1` `],` ` ` `[ ` `1` `, ` `1` `, ` `1` `] ]` ` ` `printColumnSorted(mat)` `# This code is contributed mohit kumar 29` |

*chevron_right*

*filter_none*

**Output:**

2 3 1

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.

## Recommended Posts:

- Count all the columns in a matrix which are sorted in descending
- Find row with maximum and minimum number of zeroes in given Matrix
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Count columns to be deleted to make each row sorted
- Minimize cost to convert a given matrix to another by flipping columns and reordering rows
- Convert given Matrix into sorted Spiral Matrix
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sum of columns of a 2-D Matrix where first element is odd
- Sorting rows of matrix in descending order followed by columns in ascending order
- Interchange elements of first and last columns in matrix
- Remove first X rows and columns from a matrix
- Remove any corner X rows and columns from a matrix
- Number of rows and columns in a Matrix that contain repeated values
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Enlarge a Matrix such that each element occurs in R rows and C columns
- Count rows/columns with sum equals to diagonal sum
- Count of columns with odd number of 1s
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted 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.