Modify Array

Problem Overview

  • Determine the minimum total cost to modify elements so the array becomes non-decreasing (ascending) or non-increasing (descending).
  • Input: int arr[n]; Output: an integer minimal cost; Constraints: 1 ≤ n ≤ 10^3, 1 ≤ arr[i] ≤ 10^9.
  • Cost of changing an element equals the absolute difference between its original and new value.
  • Applicable to data normalization and sequence adjustment scenarios requiring ordered outcomes.
  • Asked in Paypal interviews; a coding interview problem and common interview question.

Example

Unlock to view complete problem details

and practice with sample input/output

Was this article helpful?

View Test Cases & Run Code requires membership

Input Variables
Execution Result: