Assign Elements to Groups with Constraints - Problem

You are given an integer array groups, where groups[i] represents the size of the i-th 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.

Input & Output

Example 1 — Basic Assignment
$ Input: groups = [12, 8, 15], elements = [2, 3, 4, 5]
Output: [0, 0, 1]
💡 Note: Group 0 (size 12): 12 % 2 = 0, so assign element at index 0. Group 1 (size 8): 8 % 2 = 0, so assign element at index 0. Group 2 (size 15): 15 % 2 ≠ 0, but 15 % 3 = 0, so assign element at index 1.
Example 2 — No Valid Assignment
$ Input: groups = [7, 11], elements = [2, 4, 6]
Output: [-1, -1]
💡 Note: Group 0 (size 7): 7 % 2 ≠ 0, 7 % 4 ≠ 0, 7 % 6 ≠ 0, so assign -1. Group 1 (size 11): 11 % 2 ≠ 0, 11 % 4 ≠ 0, 11 % 6 ≠ 0, so assign -1.
Example 3 — Same Element for Multiple Groups
$ Input: groups = [6, 12, 18], elements = [3, 9]
Output: [0, 0, 0]
💡 Note: All groups are divisible by element 3 (at index 0): 6 % 3 = 0, 12 % 3 = 0, 18 % 3 = 0. Since we pick the smallest index, all groups get assigned element at index 0.

Constraints

  • 1 ≤ groups.length, elements.length ≤ 103
  • 1 ≤ groups[i] ≤ 109
  • 0 ≤ elements[i] ≤ 109

Visualization

Tap to expand
Assign Elements to Groups INPUT groups[] - Group sizes: 12 i=0 8 i=1 15 i=2 elements[] - Divisors: 2 j=0 3 j=1 4 j=2 5 j=3 Rules: 1. groups[i] % elements[j] == 0 2. Pick smallest valid index j 3. Assign -1 if no match 4. Element can repeat ALGORITHM STEPS 1 Group 0 (size=12) 12%2=0 OK at j=0 assigned[0] = 0 2 Group 1 (size=8) 8%2=0 OK at j=0 assigned[1] = 0 3 Group 2 (size=15) 15%2!=0, 15%3=0 at j=1 assigned[2] = 1 4 Early Termination Stop checking once first valid j found Divisibility Check: 12: 12/2=6 OK 8: 8/2=4 OK 15: 15/3=5 OK FINAL RESULT assigned[] output: 0 i=0 0 i=1 1 i=2 Mapping: Group 0 (12) --> elem[0]=2 Group 1 (8) --> elem[0]=2 Group 2 (15) --> elem[1]=3 Output: [0, 0, 1] All groups assigned using smallest valid indices Key Insight: Early Termination Optimization For each group, iterate through elements in index order. Stop immediately when finding a valid divisor (groups[i] % elements[j] == 0). This guarantees the smallest index j is chosen. TutorialsPoint - Assign Elements to Groups with Constraints | Early Termination Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
12.5K Views
Medium Frequency
~15 min Avg. Time
487 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen