FastPrepQuery a Header-Encoded Matrix

Query a Header-Encoded Matrix

NVIDIA logoNVIDIA● EasyFULLTIMEOA

Problem statement

The first value of arr gives the number of columns in a matrix. The remaining values list its cells row by row, from left to right.

For each query [r,c], return the cell at row r, column c. Rows and columns start at 1. Return the answers in query order. An empty query list returns an empty array.

Function

matrixQueries(arr: int[], queries: int[][]) → int[]

Examples

Example 1

arr = [3,10,20,30,40,50,60,70,80,90]queries = [[1,1],[3,2],[2,3]]return = [10,80,60]

The header 3 describes three columns. The matrix rows are [10,20,30], [40,50,60], and [70,80,90]. The requested cells contain 10, 80, and 60 in that order.

Example 2

arr = [1,-7,0,12]queries = [[3,1],[1,1],[3,1]]return = [12,-7,12]

There is one column. Queries may repeat or arrive in any row order; return the value for each query in its original position.

Constraints

  • 2 <= arr.length <= 200001.
  • Every value in arr is between -10^9 and 10^9.
  • arr[0] is a column count between 1 and 200000; the remaining values form at least one complete row.
  • 0 <= queries.length <= 200000.
  • Every query has exactly two positive integers, each at most 200000.
  • Every query [r,c] names an existing cell using one-based row and column indices.

More NVIDIA problems

See NVIDIA hiring insights
Saved
public int[] matrixQueries(int[] arr, int[][] queries) {
    // Write your solution here.
}
Checking account…