Equalize The Arrays
Make a source array equal to a target array using some given operations.
More formally, given an array source of length n, either add 1 to any prefix of the array or add 1 to any suffix of the array. That is, in one operation choose some index i and add 1 to source[0], source[1] ... source[i] or add 1 to source[i], source[i + 1]...source[n - 1].
Find the minimum number of operations required to make the arrays equal. If it is impossible to make the two arrays equal, report -1 as the answer.
Complete the function getMinOperations in the editor below.
getMinOperations has the following parameters:
int[] source: an array of integersint[] target: an array of integers
🌷 spike = 100% Awesome𓂃🖊 🌷
1Example 1
Given n = 5, source = [1,2,3,-1,0], target = [3,4,3,0,4]
- Choose i = 1 and add 1 to the prefix and perform this operation two times - source = [3,4,3,-1,0], target = [3,4,3,0,4]
- Choose i = 3 and add 1 to the suffix - source = [3,4,3,0,1], target = [3,4,3,0,4]
- Choose i = 4 and add 1 to the suffix and perform this operation three times - source = [3,4,3,0,4], target = [3,4,3,0,4]
The answer is 2 + 1 + 3 = 6.
2Example 2
3Example 3
Constraints
Limits and guarantees your solution can rely on.
🐭🐭