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𓂃🖊 🌷
source = [1, 2, 3, -1, 0] target = [3, 4, 3, 0, 4] return = 6
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.
source = [1, 2, 2] target = [2, 2, 3] return = 2
source = [1, 2, 2] target = [2, 2, 3] return = 2
🐭🐭- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
public int getMinOperations(int[] source, int[] target) {
// write your code here
}