#3447

Assign Elements to Groups with Constraints

medium · 26.8% accepted · 134 likes · top 5%

array · hash table

Description

You are given an integer array groups, where groups[i] represents the size of the ith group. You are also given an integer array elements.

Your task is to assign one element to each group based on the following rules:

- An element at index j can be assigned to a group i if groups[i] is divisible by elements[j].

- If there are multiple elements that can be assigned, assign the element with the smallest index j.

- If no element satisfies the condition for a group, assign -1 to that group.

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

Note: An element may be assigned to more than one group.

Solution