You are given an integer denoting an matrix. Initially, each cell of the matrix is empty. You are given tasks. In each task, you are given a cell where cell represents the row and column of the given matrix.
You have to perform each task sequentially in the given order. Each task is described in cell . For each task, you have to place in each cell of row and each cell column . After you complete each task, you are required to print the number of empty cells in the matrix.
Example:
Input: N=3, K=3, grid[][] = { { 1, 1 }, { 1, 3 }, { 3, 2 } };
Output: 4 2 0
Approach
Java
public class CellsInMatrix {public static void main(String[] args) {int N = 3;int K = 3;boolean arr_row[] = new boolean[N];boolean arr_column[] = new boolean[N];int arr_row_remaining_blocks = N;int arr_column_remaining_blocks = N;int grid[][] = { { 1, 1 }, { 1, 3 }, { 3, 2 } };StringBuilder sb = new StringBuilder();for (int i = 0; i < K; i++) {int row = grid[i][0] - 1;int column = grid[i][1] - 1;if (!arr_row[row]) {arr_row[row] = true;arr_row_remaining_blocks--;}if (!arr_column[column]) {arr_column[column] = true;arr_column_remaining_blocks--;}sb.append((long) arr_row_remaining_blocks * (long) arr_column_remaining_blocks + " ");}System.out.print(sb.toString());}}
No comments:
Post a Comment