Assign Elements to Groups with Constraints - Problem

You are the organizer of a team-building event where you need to assign equipment to different activity groups based on their capacity and specific rules.

Given an integer array groups where groups[i] represents the size of the i-th group, and an integer array elements containing available equipment pieces, your task is to assign one piece of equipment to each group following these rules:

  • ๐Ÿ“ An equipment piece at index j can be assigned to group i if groups[i] is divisible by elements[j]
  • ๐ŸŽฏ If multiple equipment pieces can be assigned, choose the one with the smallest index (first available)
  • โŒ If no equipment satisfies the condition for a group, assign -1 to that group
  • โ™ป๏ธ The same equipment piece can be assigned to multiple groups

Return an integer array assigned where assigned[i] is the index of the equipment chosen for group i, or -1 if no suitable equipment exists.

Example: If groups = [12, 8, 15] and elements = [2, 3, 4, 5], then group 0 (size 12) can use equipment at indices 0, 1, 2 (since 12 % 2 = 0, 12 % 3 = 0, 12 % 4 = 0), so we pick index 0 (smallest).

Input & Output

example_1.py โ€” Basic Assignment
$ Input: groups = [12, 8, 15], elements = [2, 3, 4, 5]
โ€บ Output: [0, 0, 1]
๐Ÿ’ก Note: Group 12: divisible by elements[0]=2, elements[1]=3, elements[2]=4, choose smallest index 0. Group 8: divisible by elements[0]=2, elements[2]=4, choose smallest index 0. Group 15: divisible by elements[1]=3, elements[3]=5, choose smallest index 1.
example_2.py โ€” No Valid Assignment
$ Input: groups = [7, 11], elements = [2, 4, 6]
โ€บ Output: [-1, -1]
๐Ÿ’ก Note: Group 7: not divisible by 2, 4, or 6, so assign -1. Group 11: not divisible by 2, 4, or 6, so assign -1. Both groups have prime sizes that don't match any available elements.
example_3.py โ€” All Ones
$ Input: groups = [5, 10, 15], elements = [1, 2, 3]
โ€บ Output: [0, 0, 0]
๐Ÿ’ก Note: Since 1 divides every positive integer, all groups can use elements[0]=1, which has the smallest index. Every group gets assigned index 0.

Visualization

Tap to expand
Team Equipment Assignment SystemTeam 12Team 8Team 15EquipmentType 2EquipmentType 3EquipmentType 412รท2=6โœ“8รท2=4โœ“15รท3=5โœ“Assignment ResultTeam 12 โ†’ Equipment 0 (Type 2)Team 8 โ†’ Equipment 0 (Type 2)Team 15 โ†’ Equipment 1 (Type 3)Result: [0, 0, 1]
Understanding the Visualization
1
Team Assessment
Evaluate each team's size and requirements
2
Equipment Check
For each team, check available equipment in priority order
3
Assignment
Assign the first suitable equipment piece to each team
4
Result
Each team gets optimal equipment or marked as unassignable
Key Takeaway
๐ŸŽฏ Key Insight: Always check equipment in order of priority (smallest index first) to ensure optimal assignment for each team

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * m)

Must check elements in order for each group to ensure smallest index, where n = groups.length, m = elements.length

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space beyond the result array

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค groups.length โ‰ค 103
  • 1 โ‰ค elements.length โ‰ค 103
  • 1 โ‰ค groups[i] โ‰ค 106
  • 1 โ‰ค elements[i] โ‰ค 106
  • Elements array contains no duplicates
Asked in
Google 42 Amazon 38 Microsoft 29 Meta 25
52.1K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen