#B3854. Determine Legal Memory Access in a 2D Array
Determine Legal Memory Access in a 2D Array
Determine Legal Memory Access in a 2D Array
Consider a 2D array defined in C++ as (a[N][M]), where the memory is allocated in a continuous block. Traditionally, the valid indices are (a[0\ldots N-1][0\ldots M-1]). However, due to continuous memory allocation, accessing (a[p][q]) is translated to accessing the ((p \times M + q))-th element (0-indexed) from the beginning of the array. For example, if (a[2][3]) is defined, then (a[0][4]) will not access illegal memory because it refers to the 5th element starting at (a[0][0]), which corresponds to (a[1][1]).
Given the dimensions (N) and (M) of the array and an access attempt at (a[p][q]), determine whether this access will go outside the allocated memory. In other words, let (L = p \times M + q). If (L) is within the range ([0, N \times M - 1]) then the access is legal; otherwise, it is illegal.
Note: The input integers can be assumed to be such that the computation (p \times M + q) is done using standard integer arithmetic.
inputFormat
The input consists of a single line containing four integers (N), (M), (p), and (q) separated by spaces. (N) and (M) represent the dimensions of the array and (p), (q) represent the indices attempted in the access.
outputFormat
Output a single word: output NO
if the memory access is legal (i.e. if (0 \leq p \times M + q < N \times M)); otherwise, output YES
to indicate an illegal memory access.
sample
2 3 0 4
NO