Count number of free cell present in the Matrix
Last Updated :
26 Apr, 2023
Given a Matrix size N*N and an integer K. Initially, the matrix contains only 0. You are given K tasks and for each task, you are given two coordinates (r, c). Where coordinates (r, c) denotes the rth row and the cth column of the given matrix. You have to perform each task sequentially in the given order. For each task, You have to put 1 in all rth row cells and all the cth column cells. After each task, You have to calculate the number of 0 present in the matrix and return it.
Examples:
Input: N = 3, K = 3
1 1
1 2
2 1
Output: 4 2 1
Explanation: After 1st Operation, all the cells of the 2nd row and 2nd column will be filled by 1. So, the remaining cell with the value 0 will be 4. After 2nd operation, all the second row and all the third columns will be filled by 1. So, the remaining cell with value will be 2. After 3rd operation number of cells having the value 0 will be 1.
Input: N = 2, K = 2
0 1
0 0
Output: 1 0
Explanation: After 1st operation, all the cells of the 1st row and 2nd column will be filled by 1. So, the remaining cell with the value 0 will be 1. After 2nd operation, all the cells of the 1st row and 1st column will be filled by 1. So, the remaining cell with the value 0 will be 0.
Approach: The problem can be solved based on the following observation:
Idea is, if we fill one complete row with 1 (which was initially 0), then the size of every column reduce by 1. And if we fill one complete column with 1 (which was initially 0), then the size of every row reduce by 1.
Follow the steps mentioned below to implement the idea:
- Initialize the vector, say ans, to store the number 0 after performing each operation.
- Initialize two vectors, Row_vector, and Column_vector, to store the current information about, which row and column is a fields with 1.
- Initialize two variable, say row_count and column_count, to store the number of row and column which is completely filed with 1.
- Use a loop that will be running for k times, and perform the below operation.
- Initialize two variables say, r and c inside the loop to store the row and column number on which the current operation is going to be performed.
- If the value of Row_vector at index r is 0 then increase the row_count by 1, and assign 1 to Row_vector at index r.
- If the value of Column_vector at index c is 0 then increase the column_count by 1, and assign 1 to Column_vector at index c.
- Now calculate this term (n*n)-(row_count*n)-(column_count*n)+(row_count*column_count) each time.
- Push the calculated term inside the ans vector.
- Finally, print or return the ans vector.
Below is the Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countNumberofFreeCell(vector<vector< int > >& arr, int n,
int k)
{
vector< int > ans;
vector< int > Row_vector(n, 0);
vector< int > Column_vector(n, 0);
int row_count = 0;
int column_count = 0;
for ( int i = 0; i < k; i++) {
int r = arr[i][0];
int c = arr[i][1];
if (Row_vector[r] == 0) {
row_count++;
Row_vector[r] = 1;
}
if (Column_vector == 0) {
column_count++;
Column_vector = 1;
}
ans.push_back((n * n) - (row_count * n)
- (column_count * n)
+ (row_count * column_count));
}
for ( auto & it : ans) {
cout << it << " " ;
}
cout << endl;
}
int main()
{
int n, k;
n = 3;
k = 3;
vector<vector< int > > arr(n, vector< int >(2));
arr = { { 1, 1 }, { 1, 2 }, { 2, 1 } };
countNumberofFreeCell(arr, n, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void countNumberofFreeCell( int arr[][], int n,
int k)
{
int [] ans = new int [k];
int [] Row_vector = new int [n];
int [] Column_vector = new int [n];
int row_count = 0 ;
int column_count = 0 ;
for ( int i = 0 ; i < k; i++) {
int r = arr[i][ 0 ];
int c = arr[i][ 1 ];
if (Row_vector[r] == 0 ) {
row_count++;
Row_vector[r] = 1 ;
}
if (Column_vector == 0 ) {
column_count++;
Column_vector = 1 ;
}
ans[i] = ((n * n) - (row_count * n)
- (column_count * n)
+ (row_count * column_count));
}
for ( int i = 0 ; i < k; i++) {
System.out.print(ans[i] + " " );
}
}
public static void main (String[] args) {
int n, k;
n = 3 ;
k = 3 ;
int [][] arr = { { 1 , 1 }, { 1 , 2 }, { 2 , 1 } };
countNumberofFreeCell(arr, n, k);
}
}
|
Python3
def countNumberofFreeCell(arr, n, k):
ans = []
Row_vector = [ 0 ] * n
Column_vector = [ 0 ] * n
row_count = 0
column_count = 0
for i in range (k):
r = arr[i][ 0 ]
c = arr[i][ 1 ]
if Row_vector[r] = = 0 :
row_count + = 1
Row_vector[r] = 1
if Column_vector = = 0 :
column_count + = 1
Column_vector = 1
ans.append(n * n - (row_count * n) - (column_count * n) +
(row_count * column_count))
for it in ans:
print (it, end = " " )
print ()
n = 3
k = 3
arr = [[ 1 , 1 ], [ 1 , 2 ], [ 2 , 1 ]]
countNumberofFreeCell(arr, n, k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void CountNumberofFreeCell(List<List< int >> arr, int n, int k)
{
List< int > ans = new List< int >();
List< int > Row_list = new List< int >( new int [n]);
List< int > Column_list = new List< int >( new int [n]);
int row_count = 0;
int column_count = 0;
for ( int i = 0; i < k; i++) {
int r = arr[i][0];
int c = arr[i][1];
if (Row_list[r] == 0) {
row_count++;
Row_list[r] = 1;
}
if (Column_list == 0) {
column_count++;
Column_list = 1;
}
ans.Add((n * n) - (row_count * n)
- (column_count * n)
+ (row_count * column_count));
}
foreach ( int it in ans) {
Console.Write(it + " " );
}
Console.WriteLine();
}
public static void Main()
{
int n, k;
n = 3;
k = 3;
List<List< int >> arr = new List<List< int >>();
arr.Add( new List< int >{ 1, 1 });
arr.Add( new List< int >{ 1, 2 });
arr.Add( new List< int >{ 2, 1 });
CountNumberofFreeCell(arr, n, k);
}
}
|
Javascript
function countNumberofFreeCell(arr, n, k) {
let ans = [];
let Row_vector = new Array(n).fill(0);
let Column_vector = new Array(n).fill(0);
let row_count = 0;
let column_count = 0;
for (let i = 0; i < k; i++) {
let r = arr[i][0];
let c = arr[i][1];
if (Row_vector[r] === 0) {
row_count++;
Row_vector[r] = 1;
}
if (Column_vector === 0) {
column_count++;
Column_vector = 1;
}
ans.push(
n * n - row_count * n - column_count * n + row_count * column_count
);
}
console.log(ans.join( " " ));
}
let n = 3;
let k = 3;
let arr = [
[1, 1],
[1, 2],
[2, 1],
];
countNumberofFreeCell(arr, n, k);
|
Time Complexity: O(K)
Auxiliary Space: O(N+N+K)
Share your thoughts in the comments
Please Login to comment...