FastPrepGet Minimum Cost
Problems/Get Minimum Cost

Get Minimum Cost

The D.E. Shaw Group logoThe D.E. Shaw Group● MediumOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Given an array arr of n integers, choose any two of the first three elements of the array and remove them. The cost of such an operation is the maximum of the two elements removed. For example, in the array [1, 2, 3, 4, 5], (1, 3) can be removed at a cost of 3, (1, 2) can be removed at a cost of 2 but (2, 4) cannot be removed as 4 is not amongst the first three elements of the array.

If there are less than three elements left in the array, remove all of them at a cost of the maximum of the remaining values.

Find the minimum cost to remove all the elements from the array.

Function

getMinCost(arr: int[]) → int

Complete the function getMinCost in the editor.

getMinCost has the following parameter:

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

Returns int: the minimum cost to remove all the elements from the array

Examples

Example 1

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

Among the possible options for the first step, i.e. {(3, 1), (1, 4), (3, 4)}, it is optimal to remove (3, 4) at a cost of 4. The remaining array in that case would be [1, 2]. You can now remove both the elements at a cost of 2. Thus the total cost to remove all the elements from the array is 6.

Removing any other combination in the first step, i.e. either of (3, 1) and (1, 4), would lead to a higher overall cost. For e.g. removing (3, 1) in the first step at a cost of 3 would entail removing (4, 2) in the next step at a cost of 4, leading to overall cost of 7.

Constraints

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

More The D.E. Shaw Group interview questions

  • IPOPHONE SCREEN · Seen Nov 2026
  • Print Numbers in Turn with Three ThreadsPHONE SCREEN · Seen Nov 2026
  • Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
  • Koko Eating BananasPHONE SCREEN · Seen Nov 2026
  • Sum of Smallest Non-DivisorsPHONE SCREEN · Seen Nov 2026
  • Count Lucky TicketsOA · Seen Nov 2026
  • Count Palindrome Addition ChoicesOA · Seen Nov 2026
  • Coordinate Odd and Even Workers in OrderONSITE INTERVIEW · Seen Oct 2026
  • Minimum Taps to Water a GardenONSITE INTERVIEW · Seen Oct 2026
  • Remove the Fewest Magic BeansPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Minimum Non-Adjacent Selection CapabilityMicrosoft · OAMicrosoft interview questions
  • Earn the Most 🐝Capital One · OACapital One interview questions
  • Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
  • Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
  • Minimum Cost of Left and Right PropagationAmazon · OA
  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
  • Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
  • Maximum ScoreNVIDIA · OANVIDIA interview questions
  • Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
All The D.E. Shaw Group interview questionsSee The D.E. Shaw Group hiring insights
Saved
class Solution {
    public int getMinCost(int[] arr) {
      // write your code here
    }

}
Checking account…