Interview Questions/Coding/Divisor Multiple Counts

Divisor Multiple Counts

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Divisor Multiple Counts

You are given an array of positive integers. For every element arr[i], count how many other indices j can be paired with it such that arr[j] is either a divisor of arr[i] or a multiple of arr[i]. Each index is counted at most once for arr[i], even if both conditions are true. Return the count for every index.

Examples

Example 1
Input:
2 3 4 6
Output:
2 1 1 2

Explanation: For 2, the valid values are 4 and 6. For 3, only 6 works. For 4, only 2 works. For 6, values 2 and 3 work.

Approach hint

For a value x, you need the number of array values that divide x and the number that are multiples of x.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp