Print index of columns sorted by count of zeroes in the Given Matrix
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 1
Input: 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++
#include <bits/stdc++.h>
using namespace std;
#define N 4 // rows
#define M 3 // columns
void printColumnSorted( int mat[N][M])
{
vector<pair< int , int > > colZeroCount;
for ( int i = 0; i < M; i++) {
int count = 0;
for ( int j = 0; j < N; j++) {
if (mat[j][i] == 0)
count++;
}
colZeroCount.push_back(make_pair(count, i));
}
sort(colZeroCount.begin(), colZeroCount.end());
for ( int i = 0; i < M; i++)
cout << colZeroCount[i].second + 1 << " " ;
}
int main()
{
int mat[N][M] = { { 0, 0, 0 },
{ 0, 2, 0 },
{ 0, 1, 1 },
{ 1, 1, 1 } };
printColumnSorted(mat);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int N = 4 ;
static int M = 3 ;
static void printColumnSorted( int [][] mat)
{
ArrayList<
ArrayList<Integer>> colZeroCount = new ArrayList<
ArrayList<Integer>>();
for ( int i = 0 ; i < M; i++)
{
int count = 0 ;
for ( int j = 0 ; j < N; j++)
{
if (mat[j][i] == 0 )
{
count++;
}
}
colZeroCount.add( new ArrayList<Integer>(
Arrays.asList(count,i)));
}
Collections.sort(colZeroCount,
new Comparator<ArrayList<Integer>>()
{
@Override
public int compare(ArrayList<Integer> o1,
ArrayList<Integer> o2)
{
return o1.get( 0 ).compareTo(o2.get( 0 ));
}
});
for ( int i = 0 ; i < M; i++)
{
System.out.print(
(colZeroCount.get(i).get( 1 ) + 1 ) + " " );
}
}
public static void main(String[] args)
{
int [][] mat = { { 0 , 0 , 0 }, { 0 , 2 , 0 },
{ 0 , 1 , 1 }, { 1 , 1 , 1 } };
printColumnSorted(mat);
}
}
|
Python3
N = 4
M = 3
def printColumnSorted(mat):
colZeroCount = []
for i in range (M):
count = 0
for j in range (N):
if (mat[j][i] = = 0 ):
count + = 1
colZeroCount.append((count, i))
colZeroCount = sorted (colZeroCount)
for i in range (M):
print (colZeroCount[i][ 1 ] + 1 , end = " " )
if __name__ = = '__main__' :
mat = [ [ 0 , 0 , 0 ],
[ 0 , 2 , 0 ],
[ 0 , 1 , 1 ],
[ 1 , 1 , 1 ] ]
printColumnSorted(mat)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int N = 4;
static int M = 3;
static void printColumnSorted( int [, ] mat)
{
List<Tuple< int , int > > colZeroCount = new List<Tuple< int , int > >();
for ( int i = 0; i < M; i++) {
int count = 0;
for ( int j = 0; j < N; j++) {
if (mat[j, i] == 0)
count++;
}
colZeroCount.Add(Tuple.Create(count, i));
}
colZeroCount.Sort();
for ( int i = 0; i < M; i++)
Console.Write(colZeroCount[i].Item2 + 1 + " " );
}
public static void Main( string [] args)
{
int [,] mat = { { 0, 0, 0 },
{ 0, 2, 0 },
{ 0, 1, 1 },
{ 1, 1, 1 } };
printColumnSorted(mat);
}
}
|
Javascript
<script>
let N = 4;
let M = 3;
function printColumnSorted(mat)
{
let colZeroCount = [];
for (let i = 0; i < M; i++)
{
let count = 0;
for (let j = 0; j < N; j++)
{
if (mat[j][i] == 0)
{
count++;
}
}
colZeroCount.push([count,i]);
}
colZeroCount.sort( function (a,b){ return a[0]-b[0];});
for (let i = 0; i < M; i++)
{
document.write(
(colZeroCount[i][1] + 1) + " " );
}
}
let mat = [ [ 0, 0, 0 ],
[ 0, 2, 0 ],
[ 0, 1, 1 ],
[ 1, 1, 1 ] ];
printColumnSorted(mat);
</script>
|
Complexity Analysis:
- Time Complexity: O(n*m+m*log(m))
- Auxiliary Space: O(m)
Last Updated :
07 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...