Minimum Operations to Make Arrays Equal
Updated
Problem statement
You are given two integer arrays source and target of equal length. In one operation, choose a nonempty prefix or a nonempty suffix of source and add 1 to every selected element.
Return the minimum number of operations needed to make source equal to target. If this is impossible, return -1.
Function
minOperations(source: long[], target: long[]) → longExamples
Example 1
source = [1, 2, 2]target = [2, 2, 3]return = 2First, increment the prefix containing only index 0 to change source to [2, 2, 2]. Then increment the suffix beginning at index 2 to obtain [2, 2, 3]. Therefore the minimum is 2.
Constraints
source.length = target.lengthsource.length <= 10^5-10^13 <= source[i], target[i] <= 10^13
More MathWorks interview questions
- Earliest Time to Schedule a MeetingOA · Seen Nov 2026
- Minimum Cost to Serve Requests on a RingOA · Seen Nov 2026
- Minimum Tree Diameter After Removing K LeavesOA · Seen Nov 2026
- Cities on Shortest Delivery RoutesOA · Seen Nov 2026
- Repeated Mirror XOROA · Seen Nov 2026
- Beautiful ArrangementONSITE INTERVIEW · Seen Jul 2026
- Find Minimum Cost to Remove Array ElementsONSITE INTERVIEW · Seen Jul 2026
- Group Shifted StringsONSITE INTERVIEW · Seen Jul 2026
- Longest Valid ParenthesesONSITE INTERVIEW · Seen Jul 2026
- Balancing TeamsOA · Seen Nov 2025
Similar questions at other companies
- Maximize K-Element AND with an Increment BudgetAmazon · OA
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Delete and Freeze GameRubrik · OARubrik interview questions
- Lock CodeVisa · OAVisa interview questions
- Starlink Beam PlannerSpaceX · OA · PHONE SCREENSpaceX interview questions
- Most Segments That Sort the ArrayRippling · OARippling interview questions
- Minimum Cost to Reach a Target WeightChubb · OAChubb interview questions
- Get Minimum IncrementIBM · OAIBM interview questions
- Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions