Given a list of integers and a non-negative integer k, return the number of unique index pairs (i, j) where i is less than j and the absolute difference between the values at those indices equals k. Duplicate values count each distinct index pair separately. Return 0 for null input, empty input, or null k.

Examples:

Input: nums = [1, 2, 3, 4, 5], k = 2 Output: 3 Explanation: The pairs are (1,3), (2,4), and (3,5) Input: nums = [1, 1, 1, 1], k = 0 Output: 6 Explanation: Every pair of indices has equal values, and there are 4 choose 2 equals 6 pairs Input: nums = [1, 2, 3], k = 10 Output: 0 Explanation: No pair has a difference of 10 Input: nums = [], k = 2 Output: 0 Explanation: Empty list has no pairs Input: nums = null, k = 1 Output: 0 Explanation: Null input returns 0
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay July 2026

Contest runs July 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In