3447. Assign Elements to Groups with Constraints
You are given an integer array
groups
, wheregroups[i]
represents the size of theith
group. You are also given an integer arrayelements
.Your task is to assign one element to each group based on the following rules:
- An element
j
can be assigned to a groupi
ifgroups[i]
is divisible byelements[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
, whereassigned[i]
is the index of the element chosen for groupi
, or -1 if no suitable element exists.Note: An element may be assigned to more than one group.
1 | class Solution { |