Min Operation
Updated
Problem statement
The manager of an Amazon warehouse needs to ship n products from different locations, the location of the ith product is represented by an array locations[i]. The manager is allowed to perform one operation at a time. Each operation is described below:
- If the inventory has two or more products, the manager can pick two products x and y from the inventory if they have different locations (locations[x]!=locations[y]) and ship both of them.
- If the inventory has one or more products, the manager can pick one product x from the inventory and ship it.
Note: After shipping a product it gets removed from the inventory, and the rest of the products which are currently not shipped come together keeping the order the same as before.
Given n products and an array locations, find the minimum number of operations that the manager has to perform to ship all of the products.
Function
minOperation(m: int, locations: int[]) → intComplete the function minOperation in the editor.
minOperation has the following parameter(s):
int locations[n]: the location of each product.
Returns int: the minimum number of operations that the manager has to perform to ship all of the products.
Examples
Example 1
m = 5locations = [1, 8, 6, 7, 7]return = 3One optimal sequence is to ship products from different locations together whenever possible. For [1, 8, 6, 7, 7], pair locations 1 and 8, then pair 6 and one 7, then ship the remaining 7 alone. This takes 3 operations.
Example 2
m = 4locations = [1, 3, 1, 2]return = 2Pair the products at locations 1 and 3, then pair the remaining products at locations 1 and 2. This ships all products in 2 operations.
Constraints
1 <= m <= 105locations.length == m1 <= locations[i] <= 109
More Amazon interview questions
- Get Smallest Base SegmentOA · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- 3SumPHONE SCREEN · Seen Nov 2026
- Circular Primes Below a LimitPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Maximum Distinct Values After SwapsAkuna Capital · OAAkuna Capital interview questions
- Min Cost to Complete All ProjectsSalesforce · OASalesforce interview questions
- Freelancing PlatformJP Morgan Chase · OAJP Morgan Chase interview questions
- Maximum Distinct Values After Cross-Array SwapsScale AI · OAScale AI interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimum Distinct Prefix CostChubb · OAChubb interview questions
- Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
- Rearrange Songs by AuthorZomato / Eternal · OAZomato / Eternal interview questions
- 🐘 Keep Them Apart 🦙QRT · OA
- Array Reduction AlgorithmSnowflake · OASnowflake interview questions
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences