Question

In: Computer Science

in javascript OR JAVA You are given two numbers n and m representing the dimensions of...

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:

  • [0] - find the minimum value among all remaining active cells on the board.
  • [1, i] - deactivate all cells in row i;
  • [2, j] - deactivate all cells in column j;

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.

Solutions

Expert Solution

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:

  • [0] - find the minimum value among all remaining active cells on the board.
  • [1, i] - deactivate all cells in row i;
  • [2, j] - deactivate all cells in column j;

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.

  • So what I assumed is, to return all those minimum values which were deactivated by the query ,   [ 0 ] -- find the minimum value among all remaining active cells on the board.
  • So I returned an array of size queries.length ( if you want you can modify that size, but it will have a zero ( 0 ) value in the empty spaces in the array )

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 !.


Related Solutions

Split the Number IN JAVASCRIPT Programming challenge description: You are given a number N and a...
Split the Number IN JAVASCRIPT Programming challenge description: You are given a number N and a pattern. The pattern consists of lowercase latin letters and one operation "+" or "-". The challenge is to split the number and evaluate it according to this pattern e.g. 1232 ab+cd -> a:1, b:2, c:3, d:2 -> 12+32 -> 44 Input: Your program should read lines from standard input. Each line contains the number and the pattern separated by a single whitespace. The number...
In Java please You are given a matrix of characters representing a big box. Each cell...
In Java please You are given a matrix of characters representing a big box. Each cell of the matrix contains one of three characters: " / "which means that the cell is empty; '*', which means that the cell contains an obstacle; '+', which means that the cell contains a small box. You decide to rotate the big box clockwise to see how the small boxes will fall under the gravity. After rotating, each small box falls down until it...
THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers...
THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that randomly generates 16 numbers, and it assigns them to the array after testing that the number was not already assigned. The program should test whether they form a...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the number of rows and columns, respectively, define a function named sqabsmax that accepts array A as an argument (i.e. input parameter) and returns the square of the maximum absolute value element in A. Use the const qualifier if appropriate. Only show the function definition. Do not write an entire program with a main function. Just write the definition for function sqabsmax. in C
Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is...
Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is a function of x + y. That is, let f(t) be a function such that f(x + y) = (∂N/∂x − ∂M/∂y)/(M − N) Assume that you can solve the differential equation M dx + N dy = 0 by multiplying by an integrating factor μ that makes it exact and that it can also be written as a function of x + y,...
Three Patterns Given two numbers N and K, output three squares with the size N ×...
Three Patterns Given two numbers N and K, output three squares with the size N × N each with their own set of rules: • The first square is made entirely using the ‘#’ symbol. • The second square is made using the ‘.’ symbol except for every K rows use the ‘#’ symbol instead. • The third square is made using the ‘.’ symbol except for every K columns use the ‘#’ symbol instead. Also print new line (\n)...
Given the relation R = {(n, m) | n, m ∈ ℤ, n ≥ m}. Which...
Given the relation R = {(n, m) | n, m ∈ ℤ, n ≥ m}. Which of the following statements about R is correct? R is not a partial order because it is not antisymmetric R is not a partial order because it is not reflexive R is a partial order R is not a partial order because it is not transitive
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are...
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are smaller than n and have a digit 7. For example, if n is 100, the program should output 7, 17, 37, 47, 67, 71, 73, 79, and 97.
1. Prove that given n + 1 natural numbers, there are always two of them such...
1. Prove that given n + 1 natural numbers, there are always two of them such that their difference is a multiple of n. 2. Prove that there is a natural number composed with the digits 0 and 5 and divisible by 2018. both questions can be solved using pigeonhole principle.
A flat surface of dimensions 2 m by 2 m that is given a total charge...
A flat surface of dimensions 2 m by 2 m that is given a total charge of 400 μC uniformly distributed over the surface, and where a point charge +4 μC is fixed a distance 2 cm above the centre of the charged surface. A second point charge +16 nC, with mass 10 g, is placed at rest directly above the first point charge, at a distance 2 cm from the first point charge. The second point charge is released...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT