#3447
Assign Elements to Groups with Constraints
expert · 1150 · lc medium +32 · 26.8% accepted · 134 likes · top 5%
Description
Given integer arrays groups and elements, assign exactly one element to each group using these rules:
- Element at index j qualifies for group i if groups[i] is evenly divisible by elements[j].
- Among all qualifying elements, choose the one with the smallest index j.
- If no element qualifies for a group, assign -1 to it.
Return an array assigned where assigned[i] is the chosen element index for group i, or -1 if none qualifies.
Note: The same element may serve multiple groups.
Code
1
2
3