Find Total Weight
Updated
Problem statement
Stacey is coordinating a beach clean-up event with her university's Women in STEM Charity branch. The beach is covered with tin cans of varying weights arranged in a single line, indexed from 0 to n-1.
Stacey uses a scooper that can pick up three adjacent cans at a time. For each selection:
- She identifies the lightest remaining can, with weight w
- She uses the scooper to pick up that can along with its two adjacent cans (or fewer if at the edge)
- She continues this process until there are no cans left on the beach
If multiple cans have the lightest weight, Stacey selects the one with the smallest index. If a can has fewer than two adjacent cans, she removes the available adjacent cans.
Determine the sum of the weights of the lightest cans she picks in each selection.
Function
findTotalWeight(cans: int[]) → int
Complete the function findTotalWeight in the editor with the following parameters:
int cans[n]: the weights of the cans on the beach
Examples
Example 1
cans = [5, 4, 1, 3, 2]return = 3Let there be n = 5 cans on the beach with weights represented by cans = [5, 4, 1, 3, 2].
- First, choose the minimum weight, i.e., 1, and add that weight to the total. The cans with weights 4, 1, and 3 are removed. The array of cans is now [5, 2].
- Then choose the minimum weight, i.e., 2, and add that weight to the total. The cans with weights 2 and 5 are removed, and there are no more cans on the beach.
Hence, the total is 1 + 2 = 3.
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
- Find Total CostOA · Seen Oct 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
Similar questions at other companies
- Repeated Maximum and Minimum Request ScoreAmazon · OA
- Update Pod Counts From LogsSalesforce · OASalesforce interview questions
- Apply Binary State OperationsZipRecruiter · OAZipRecruiter interview questions
- Shortest-Job-First Process SchedulingByteDance · PHONE SCREENByteDance interview questions
- Dynamic Batch Completion TimesAmbience Healthcare · PHONE SCREEN
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
- Cursor-Based Pagination Over Sorted LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Issue and Reuse 16-Bit TokensMeta · ONSITE INTERVIEWMeta interview questions
- Finish Ordered CPU Tasks with the Fewest CPUsGoogle · PHONE SCREENGoogle interview questions
- Weighted Server Load Balancer with TTLStripe · ONSITE INTERVIEWStripe interview questions
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)