Open In App

Count number of free cell present in the Matrix

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function used to calculate the
// number of free cell present inside
// the matrix after each operation
void countNumberofFreeCell(vector<vector<int> >& arr, int n,
                           int k)
{
 
    // Answer vector to store the number
    // of 0 present in side the matrix
    // after each operation.
    vector<int> ans;
 
    // Row vector use to store
    // which row is completely filled
    // with 0.
    vector<int> Row_vector(n, 0);
 
    // Column vector use to store
    // which row is completely filled
    // with 0.
    vector<int> Column_vector(n, 0);
 
    // Use to count number row in matrix
    // which is completely filled with 1.
    int row_count = 0;
 
    // Use to count number column in
    // matrix which is completely
    // filled with 1.
    int column_count = 0;
 
    for (int i = 0; i < k; i++) {
 
        // Row and Column on which operation
        // is going to be performed.
        int r = arr[i][0];
        int c = arr[i][1];
 
        // 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 (Row_vector[r] == 0) {
            row_count++;
            Row_vector[r] = 1;
        }
 
        // 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.
        if (Column_vector == 0) {
            column_count++;
            Column_vector = 1;
        }
 
        // Here n*n denotes the total
        // matrix size and
        // (row_count*column_count)
        // denotes how many intersection
        // is present in the calculation
        ans.push_back((n * n) - (row_count * n)
                      - (column_count * n)
                      + (row_count * column_count));
    }
 
    // Print the answer vector
    for (auto& it : ans) {
 
        cout << it << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
 
    int n, k;
    n = 3;
    k = 3;
 
    // Initialize the arr vector
    // which contains the row number
    // and column number
    vector<vector<int> > arr(n, vector<int>(2));
    arr = { { 1, 1 }, { 1, 2 }, { 2, 1 } };
 
    // Function call
    countNumberofFreeCell(arr, n, k);
 
    return 0;
}


Java




// Java code of the above approach
 
import java.io.*;
 
class GFG {
 
// Function used to calculate the
// number of free cell present inside
// the matrix after each operation
static void countNumberofFreeCell(int arr[][], int n,
                           int k)
{
 
    // Answer vector to store the number
    // of 0 present in side the matrix
    // after each operation.
    int[] ans = new int[k];
 
    // Row vector use to store
    // which row is completely filled
    // with 0.
    int[] Row_vector = new int[n];
 
    // Column vector use to store
    // which row is completely filled
    // with 0.
    int[] Column_vector = new int[n];
 
    // Use to count number row in matrix
    // which is completely filled with 1.
    int row_count = 0;
 
    // Use to count number column in
    // matrix which is completely
    // filled with 1.
    int column_count = 0;
 
    for (int i = 0; i < k; i++) {
 
        // Row and Column on which operation
        // is going to be performed.
        int r = arr[i][0];
        int c = arr[i][1];
 
        // 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 (Row_vector[r] == 0) {
            row_count++;
            Row_vector[r] = 1;
        }
 
        // 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.
        if (Column_vector == 0) {
            column_count++;
            Column_vector = 1;
        }
 
        // Here n*n denotes the total
        // matrix size and
        // (row_count*column_count)
        // denotes how many intersection
        // is present in the calculation
        ans[i] = ((n * n) - (row_count * n)
                      - (column_count * n)
                      + (row_count * column_count));
    }
 
    // Print the answer vector
    for (int i = 0; i < k; i++) {
 
        System.out.print(ans[i] + " ");
    }
}
 
// Driver code
 
    public static void main (String[] args) {
          int n, k;
          n = 3;
          k = 3;
 
          // Initialize the arr vector
          // which contains the row number
          // and column number
          int[][] arr = { { 1, 1 }, { 1, 2 }, { 2, 1 } };
 
          // Function call
          countNumberofFreeCell(arr, n, k);
 
    }
}


Python3




# Python code of the above approach
 
# Function used to calculate the
# number of free cell present inside
# the matrix after each operation
def countNumberofFreeCell(arr, n, k):
     
    # Answer array to store the number
    # of 0 present in side the matrix
    # after each operation.
    ans = []
     
    # Row array use to store
    # which row is completely filled
    # with 0.
    Row_vector = [0]*n
     
    # Column array use to store
    # which row is completely filled
    # with 0.
    Column_vector = [0]*n
     
    # Use to count number row in matrix
    # which is completely filled with 1.
    row_count = 0
     
    # Use to count number column in
    # matrix which is completely
    # filled with 1.
    column_count = 0
     
    for i in range(k):
         
        # Row and Column on which operation
        # is going to be performed.
        r = arr[i][0]
        c = arr[i][1]
         
        # 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 Row_vector[r] == 0:
            row_count += 1
            Row_vector[r] = 1
             
        # 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.
        if Column_vector == 0:
            column_count += 1
            Column_vector = 1
         
        # Here n*n denotes the total
        # matrix size and
        # (row_count*column_count)
        # denotes how many intersection
        # is present in the calculation
        ans.append(n*n - (row_count * n) - (column_count * n) +
        (row_count * column_count))
     
    # Print the answer array
    for it in ans:
        print(it, end=" ")
    print()
 
# Driver code
n = 3
k = 3
 
# Initialize the arr array
# which contains the row number
# and column number
arr = [[1, 1], [1, 2], [2, 1]]
 
# Function call
countNumberofFreeCell(arr, n, k)
 
# This code is contributed by Prasad Kandekar(prasad264)


C#




// C# code of the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
    static void CountNumberofFreeCell(List<List<int>> arr, int n, int k)
    {
        // Answer list to store the number
        // of 0 present inside the matrix
        // after each operation.
        List<int> ans = new List<int>();
 
        // Row list use to store
        // which row is completely filled
        // with 0.
        List<int> Row_list = new List<int>(new int[n]);
 
        // Column list use to store
        // which row is completely filled
        // with 0.
        List<int> Column_list = new List<int>(new int[n]);
 
        // Use to count number row in matrix
        // which is completely filled with 1.
        int row_count = 0;
 
        // Use to count number column in
        // matrix which is completely
        // filled with 1.
        int column_count = 0;
 
        for (int i = 0; i < k; i++) {
 
            // Row and Column on which operation
            // is going to be performed.
            int r = arr[i][0];
            int c = arr[i][1];
 
            // If the value of Row_list at
            // index r is 0 then increase the
            // row_count by 1, and assign 1
            // to Row_list at index r.
            if (Row_list[r] == 0) {
                row_count++;
                Row_list[r] = 1;
            }
 
            // If the value of Column_list
            // at index c is 0 then increase
            // the column_count by 1, and
            // assign 1 to Column_list
            // at index c.
            if (Column_list == 0) {
                column_count++;
                Column_list = 1;
            }
 
            // Here n*n denotes the total
            // matrix size and
            // (row_count*column_count)
            // denotes how many intersection
            // is present in the calculation
            ans.Add((n * n) - (row_count * n)
                      - (column_count * n)
                      + (row_count * column_count));
        }
 
        // Print the answer list
        foreach (int it in ans) {
            Console.Write(it + " ");
        }
        Console.WriteLine();
    }
 
    // Driver code
    public static void Main()
    {
        int n, k;
        n = 3;
        k = 3;
 
        // Initialize the arr list
        // which contains the row number
        // and column number
        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 });
 
        // Function call
        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);


Output

4 2 1 

Time Complexity: O(K)
Auxiliary Space: O(N+N+K)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads