Find Total Cost
Updated
Problem statement
Given an array arr of n integers, a sequence of n-1 operations must be performed on the array.
In each operation,
- Remove the minimum and maximum elements from the current array and add their sum back to the array.
- The cost of an operation,
cost = ceil((minimum_element + maximum_element) / (maximum_element-minimum_element + 1)).
Find the total cost to reduce the array to a single element.
Function
findTotalCost(arr: int[]) → intExamples
Example 1
arr = [2, 3, 4, 5, 7]return = 8- Choose
2and7: costceil(9 / 6) = 2. The array becomes[3,4,5,9]; total cost is2. - Choose
3and9: costceil(12 / 7) = 2. The array becomes[4,5,12]; total cost is4. - Choose
4and12: costceil(16 / 9) = 2. The array becomes[5,16]; total cost is6. - Choose
5and16: costceil(21 / 12) = 2. The array becomes[21]; total cost is8.
Constraints
2 <= arr.length <= 2 * 10^5.1 <= arr[i] <= 10^6.
More JP Morgan Chase interview questions
- Longest Consecutive SequencePHONE SCREEN · Seen Nov 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Binary XOR with a Set-Bit LimitPHONE SCREEN · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
Similar questions at other companies
- Split Tender Part 2 - Expiration DatesStripe · OAStripe interview questions
- Organization Reputation After Employee DeparturesHSBC · OAHSBC interview questions
- Last Object Exit TimeIBM · OAIBM interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Bold Play to Reach a GoalSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Macaron Order PriceOpenAI · OAOpenAI interview questions
- Daily Settlement for Long and Short PositionsBelvedere Trading · OA
JP Morgan Chase interview experiences
- JPMorgan Chase Experienced SWE Java Interview: 1 Experience
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)