Skip to content
Onsite

Can You Find the Sum

MediumP3Coding

Return true if some non-empty contiguous subarray of nums adds up to exactly k, otherwise false. Elements may be negative.

Example 1

Input
nums = [1,2,3], k = 5
Output
true
Why
The subarray [2,3] sums to 5.

Example 2

Input
nums = [1,2,3], k = 7
Output
false
Why
No contiguous run adds up to 7 (the whole array is 6).

Example 3

Input
nums = [], k = 0
Output
false

Constraints

  • 0 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • -10^9 <= k <= 10^9

Hints

Run checks the 3 visible tests, Submit all 12.