I'm working on a problem where I need to construct an array of size N such that the array adheres to the following constraints:
The first element of the array is always 0 (arr[0] = 0). The difference between adjacent elements is less than or equal to 1 (|arr[i] - arr[i-1]| <= 1). We are given M pairs (x, y), which specify that the value at index x cannot exceed y (arr[x] <= y). All the values of array must be positive integers Given these constraints, I need to determine the maximum possible value in the array.
Here are some example test cases:
N = 5, M = 4 with pairs (1, 0), (2, 0), (3, 0), (4, 0) Possible array: [0, 0, 0, 0, 0] Answer: 0
N = 5, M = 1 with pair (3, 0) Possible array: [0, 1, 1, 0, 1] Answer: 1
N = 10, M = 1 with pair (3, 1) Possible array: `[0, 1, 2, 1,2,3,4,5,6] answer is 6
fourth test case N = 10, M =2 (1,1) (3,4)
array : [0,1,2,3,4] answer is 4
I tried but failed on some test cases
example: N = 10, M = 3 (3,20) (7,50) (9,1)