Skip to the code
cf.
+10 XP

#28 Two Prices Exactly K Apart

easy

A pricing page wants to show a "save exactly k" badge when one plan costs exactly k more than another.

Write differByK(prices, k) that returns true if there are two different indexes i and j with prices[i] - prices[j] === k, and false otherwise.

Example 1

Input: prices = [10,25,17,32], k = 15
Output: true

25 - 10 = 15.

Example 2

Input: prices = [1,5,9], k = 3
Output: false

The differences are 4, 8, and 4.

Constraints

  • 0 <= prices.length <= 10^5
  • 0 <= k <= 10^9
  • -10^9 <= prices[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).