FastPrepModify Array
Problems/Modify Array

Modify Array

PayPal logoPayPal● HardOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Given an array of integers, the cost to change an element is the absolute difference between its initial value and its new value. For example, if the element is initially 10, it can be changed to 7 or 13 for a cost of 3. Determine the minimum cost to sort the array either ascending or descending along its length.

Function

modifyArray(arr: int[]) → int

Complete the function modifyArray in the editor.

modifyArray has the following parameters:

  1. int arr[n]: an array of integers

Returns int: an integer that denotes the minimum cost required to make the array ascending (non-decreasing) or descending (non-increasing) along its length.

Examples

Example 1

arr = [9, 8, 7, 2, 3, 3]return = 1

N/A :)

Example 2

arr = [1, 2, 3, 3, 4]return = 0

The array arr is already sorted in ascending order. The minimum cost to sort it in ascending order is 0.

Example 3

arr = [0, 1, 2, 5, 6, 5, 7]return = 1

Only arr[5] = 5 violates the order for an ascending sort. If the value 5 is increased to 6, the arr will be sorted in ascending order: arr' = [0, 1, 2, 5, 6, 6, 7] The cost is |arr[5] - [5 - 6]| = 1 :)

Constraints

  • 1 ≤ n ≤ 10^3
  • 1 ≤ arr[i] ≤ 10^9

More PayPal interview questions

  • Minimum Cost to Merge Values into OneOA · Seen Nov 2026
  • Per-User Sliding-Window Rate LimiterOA · Seen Nov 2026
  • Total Cost Against the Lowest Earlier PriceOA · Seen Nov 2026
  • Can the Singer Perform the Song?PHONE SCREEN · Seen Jul 2025
  • Items Purchase (For Java Backend Engineer)OA · Seen Nov 2024
  • Find Minimum Price to SpendOA · Seen Oct 2024
  • Count User Logins 🌿 (Singapore)OA · Seen Mar 2024
  • Similar Password 🪴 (Singapore)OA · Seen Mar 2024

Similar questions at other companies

  • At Most K Jumps with Quadratic Transition CostInfosys · OAInfosys interview questions
  • Largest Consistent Visiting GroupAuxoAI · OA
  • Delete K Subsets for Maximum ScoreRubrik · OARubrik interview questions
  • Optimize Discount Selection Under a Point BudgetPalantir · OAPalantir interview questions
  • Maximum Index-Weighted Sum with Disjoint SwapsVisa · OAVisa interview questions
  • Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
  • Pressure-Isolation Shutdown OrderIntuit · OAIntuit interview questions
  • Get Minimum IncrementIBM · OAIBM interview questions
  • Jump Game with Prime-3 StepsUber · OAUber interview questions
  • Maximum Payout with Context SwitchesPhonePe · OAPhonePe interview questions

PayPal interview experiences

  • PayPal SWE Intern OA Questions: 1 Experience
All PayPal interview questionsSee PayPal hiring insights
Saved
class Solution {
    public int modifyArray(int[] arr) {
      // write your code here
    }

}
Checking account…