FastPrepMinimum Operations to Make Arrays Equal
Problems/Minimum Operations to Make Arrays Equal

Minimum Operations to Make Arrays Equal

MathWorks logoMathWorks● HardFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: 🐱 Corrected on 2026-07-18. The reported source permits values up to 10^13, so the runnable signature now uses long[] inputs and a long result. The judged core task matches about 99% of the reported source.

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[]) → long

Examples

Example 1

source = [1, 2, 2]target = [2, 2, 3]return = 2

First, 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.length
  • source.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

MathWorks interview experiences

  • MathWorks SWE Intern Phone Screens: 1 Experience
  • MathWorks Software Engineer in Test OA and HireVue: 1 Experience
All MathWorks interview questionsSee MathWorks hiring insights
Saved
class Solution {
    public long minOperations(long[] source, long[] target) {
      // write your code here
    }

}
Checking account…