Problem Β· Array
Array Nullification
Learn this problemProblem statement
You are given two arrays, change and arr, of lengths n and m, respectively. In each operation i, you can perform one of the following:
- You can decrement any element of
arrby1. - If
change[i] > 0andarr[change[i]] = 0, you can change that element toNULL.
Assume 1-based indexing and find the minimum number of operations required to change all elements of the array to NULL, or report -1 if not possible.
Function
getMinOperations(change: int[], arr: int[]) β intComplete the function getMinOperations in the editor with the following parameters:
int change[n]: an array of integersint arr[m]: an array of integers
Returns:
int: the minimum number of operations required to change all the elements toNULL, or-1if it is not possible
Examples
Example 1
change = [0, 1, 0, 2]arr = [1, 1]return = 4Operations sequence:
- Decrement
arr[1]:[0, 1] - Change
arr[1]toNULLsincechange[2] = 1andarr[1] = 0:[NULL, 1] - Decrement
arr[2]:[NULL, 0] - Change
arr[2]toNULLsincechange[4] = 2andarr[2] = 0:[NULL, NULL]
The answer is 4.
Constraints
1 <= n <= 10^5