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
jcan be assigned to groupiifgroups[i]is divisible byelements[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
-1to 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
Visualization
Time & Space Complexity
Must check elements in order for each group to ensure smallest index, where n = groups.length, m = elements.length
Only using constant extra space beyond the result array
Constraints
- 1 โค groups.length โค 103
- 1 โค elements.length โค 103
- 1 โค groups[i] โค 106
- 1 โค elements[i] โค 106
- Elements array contains no duplicates