Amazon Interview Question

Given an array of integers A. calculate the sum of A[i] %A[j] for all possible i,j pair. return sum%(10^9+7) as an output solve this problem on o(n).
Example:
Input: arr[] = {1, 2, 3}
Output: 5
(1 % 1) + (1 % 2) + (1 % 3) + (2 % 1) + (2 % 2)

  • (2 % 3) + (3 % 1) + (3 % 2) + (3 % 3) = 5

Input: arr[] = {1, 2, 4, 4, 4}
Output: 10

Comments (35)