Skip to content
Onsite

Sum of Absolute Differences at Prime Distance

MediumP3Coding

Given an array nums, return the sum of |nums[i] - nums[j]| over all pairs of indices i < j for which the distance j - i is a prime number.

Example 1

Input
nums = [1,5,2,9]
Output
13
Why
Distances 2 and 3 are prime: |1-2| + |5-9| + |1-9| = 1 + 4 + 8 = 13.

Example 2

Input
nums = [7]
Output
0

Example 3

Input
nums = [3,3,3,3]
Output
0

Constraints

  • 1 <= nums.length <= 2000
  • 0 <= nums[i] <= 10^6

Hints

Run checks the 3 visible tests, Submit all 10.