Card Packets
Learn this problemProblem statement
An e-commerce company specializes in cards with sports figures on them. Each sport has different categories of cards. For instance, there might be more desirable cards with the most popular sports personalities, others with small pieces of a player's jersey attached, and so on. They have a number of each category of card and want to make some number of packets greater than 1 that each contain equal numbers of each type of card. To do this, they will add more cards of each type until each type can be divided equally among some number of packets. Determine the minimum number of additional cards needed to create a number of packets with equal type distribution.
Function
cardPackets(cardTypes: int[]) → int
Complete the function cardPackets in the editor.
cardPackets has the following parameter(s):
int cardTypes[n]: the quantity available of card type
Returns
int: the minimum number of additional cards to add
Examples
Example 1
cardTypes = [4, 7, 5, 11, 15]return = 4In order to make 2 matching packets, the following numbers of additional cards must be added: [0, 1, 1, 1, 1]. This sums to 4 additional cards. The numbers of cards are [4, 8, 6, 12, 16] and they can be divided evenly among 2 packets. If 3 packets are created, an additional [2, 2, 1, 1, 0] cards are needed, sum = 6 items. This yields quantities [6, 9, 6, 12, 15]. Any number of packets ≥ 2 can be created, but creating 2 packets requires the minimum number of additional cards.
Example 2
cardTypes = [3, 8, 7, 6, 4]return = 2For 2 packets add: [1, 0, 1, 0, 0] (2 cards) to get [4, 8, 8, 6, 4]. For 3 add [0, 1, 2, 0, 2] (5 cards) to get [3, 9, 9, 6, 6]. Any number of packets ≥ 2 can be created, but making 2 packets requires the minimum number of additional cards.
Constraints
- 1 ≤ n ≤ 10^5
- 1 ≤ cardTypes[i] ≤ 500
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026