Skip to content

Practice Assign Elements to Groups with Constraints

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3447
LeetCode Problem

Assign Elements to Groups with Constraints

Problem Statement

For each size groups[i], choose the smallest index j for which elements[j] divides groups[i] evenly. Return those selected indices, using -1 when no element divides a group’s size. The same element may be assigned to multiple groups.

Source: Assign Elements to Groups with Constraints on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
groups = [8,4,3,2,4], elements = [4,2]
Output
[0,0,-1,1,0]

# Example 2

Input
groups = [2,3,5,7], elements = [5,3,3]
Output
[-1,1,0,-1]

# Example 3

Input
groups = [10,21,30,41], elements = [2,1]
Output
[0,1,0,1]

Constraints

1 <= groups.length <= 10^5
1 <= elements.length <= 10^5
1 <= groups[i] <= 10^5
1 <= elements[i] <= 10^5

Related LeetCode Problems

Practice more problems similar to Assign Elements to Groups with Constraints.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.