In: Computer Science
in javascript OR JAVA
You are given two numbers n and m representing the dimensions of an n × m rectangular board. The rows of the board are numbered from 1 to n, and the columns are numbered from 1 to m. Each cell has a value equal to the product of its row index and column index (both 1-based); in other words, board[i][j] = (i + 1) * (j + 1).
Initially, all the cells in the board are considered active, though some of them will eventually be deactivated through a sequence of queries - specifically, you will be given an array queries, where each query is of one of the following 3 types:
Given the dimensions n, m, and the array of queries, your task is to return an array consisting of calculated values (results of the queries of the 0th type), in the order in which they were calculated.
Hey , i am solving this problem using JAVA.
Actually I am sorry to say this, The question is not very much clear. But still I tried my best to solve the question. If you feel something has been missed or you want me to change some code , you can ask me in the comment section. I will make changes ASAP.
Assumptions I made:
1. ACTIVE cell means the cell with NON - ZERO value. ( Because as per the question , no cell will be initialized with ZERO value , (i+1)*(j+1) where i and j starts from 0 ).
2. DEACTIVATED cell means , the cell with value ZERO. So I made that cell value as ZERO.
3. Array of Queries:
you will be given an array queries, where each query is of one of the following 3 types:
So, What I did is I made a 2 dimensional queries array.
4. your task is to return an array consisting of calculated values (results of the queries of the 0th type), in the order in which they were calculated.
Now I am providing the code here:
===========================================================================================================================================================
import java.util.*;
public class Main {
// main( ) method:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// ask user to enter the no.of rows
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
// ask user to enter the no.of columns
System.out.print("\nEnter the number of columns: ");
int cols = sc.nextInt();
int[][] board = new int[rows][cols];
// initialization of values on board
for(int i=0; i<rows; i++) {
for(int j=0; j<cols;j++) {
board[i][j] = (i+1)*(j+1);
}
}
// end of for loop ( intializiation completed)
// array of queries
int[][] queries = new int[3][];
queries[0] = new int[] {0};
// [1,i] to deactivate all cells in 'i'th row.
queries[1] = new int[]{1,1};
// [2,j] to deactivate all cells in 'j'th column
queries[2] = new int[]{2,2};
int[] res = solve(queries , board);
for(int z =0; z< res.length;z++) {
System.out.println("calculated values (results of the queries of
the 0th type): "+ res[z]);
}
}
// ACTIVE CELLS === Cells whose
value is NOT zero
// DEACTIVATED CELLS ==== Cells whose value is
ZERO.
public static int[] solve(int[][] queries , int[][] board )
{
int[] result = new int[queries.length] ;
int z =0;
for(int i = 0; i<queries.length;i++) {
// find the minimum value among all
remaining active cells on the board.
if(queries[i][0] == 0) {
// initialize ' min ' variable with some very high value.
int min = 1000000;
int min_row = 0,min_col = 0 ;
for(int x =0; x < board.length; x++) {
for(int y =0; y < board[i].length; y++) {
if(board[x][y] < min ) {
min = board[x][y];
min_col = y;
min_row = x;
}
}
} // for loop END ( found a minimum
value) and it's indexes.
result[z] = min;z++;
board[min_row][min_col] = 0;
}
// [1,i] === deactivate all cells in row i;
else if(queries[i][0] == 1) {
int x = queries[i][1];
for(int k =0; k < board[0].length; k++) {
board[x][k] = 0;
}
}
// [2,j] ===== deactivate all cells in column j;
else if(queries[i][0] == 2 ) {
int y = queries[i][1];
for(int k=0; k < board.length; k++) {
board[k][y] = 0;
}
}
}
return result;
}
}
==============================================================
OUTPUT
The first value is 1 , because the minimum value is 1. ( i.e.. i =0, j =0. the value at a[ i ][ j ] = ( i + 1 ) * ( j + 1) = 1
And the other two values are 0 because , the size of array is queries.length ( which is 3 in our case ). So the default value in the array is zero.
But last two values is not because of any query.
I hope you got your answer, if not please do comment I will modify code as per your request. THANK YOU !.