Minimize Adjacent Squared-Difference Cost with One Insertion
Updated
Problem statement
The cost of an integer array is the sum of (values[i]-values[i-1])² over all adjacent pairs.
Insert exactly one integer at any position, including either end, and return the minimum possible cost. The inserted integer may be any signed 32-bit value.
Function
minimumSquaredDifferenceCost(values: int[]) → longExamples
Example 1
values = [1,4,7,4]return = 23Insert 5 between 4 and 7: 9+1+4+9=23.
Example 2
values = [1,5]return = 8Insert 3 and split a gap of four into two gaps of two.
Example 3
values = []return = 0After insertion, an originally empty array still has no adjacent pair.
Constraints
0 <= values.length <= 100000.-1000000000 <= values[i] <= 1000000000.- The answer fits in a signed 64-bit integer.
More Scale AI Online Assessment questions
- Part 2: Batch Access ReviewOA · Seen Nov 2026
- Part 1: The Narrowest KeyOA · Seen Nov 2026
- Build Sessions from Unordered Events (Part 1)OA · Seen Oct 2026
- Match Billing Charges to User Sessions (Part 2)OA · Seen Oct 2026
- Four-Player Trick-Taking Card Game SimulationOA · PHONE SCREEN · Seen Aug 2026
- Free Windows Between Busy IntervalsOA · PHONE SCREEN · Seen Apr 2026
- Compare Labeling Worker AnnotationsOA · Seen Mar 2025
- Count Stable Server SubsegmentsOA · Seen Nov 2024
- Distinct OR Values of Increasing SubsequencesOA · Seen Nov 2024
- Running Through Walls with Limited EnergyOA · Seen Sep 2024
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Values with Even Powers of TwoVisa · OAVisa interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMicrosoft · OAMicrosoft interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Resource AllocationMaven Securities · OAMaven Securities interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Total Batch ExpenseAmazon · OA
- Coprime Pick with Change CostCitadel · OACitadel interview questions