ERICSSON On Campus OA 2022 | Number of Subarrays With Sum Equals K (Exact) | Prefix Sum and HashMap

Question · Posted Jun 2026

Problem Statement You are given a binary array nums and an integer goal. Return the number of non-empty subarrays whose sum is exactly equal to goal. A subarray is a contiguous part of an array. Example 1 Input: nums = [1,0,1,0,1] goal = 2 Output: 4 Explanation: The valid subarrays are: [1,0,1] [1,0,1,0] [0,1,0,1] [1,0,1] Hence, the answer is 4. Example 2 Input: nums = [0,0,0,0,0] goal = 0 Output: 15 Constraints 1 <= nums.length <= 3 * 10^4 nums[i] ...

The full answer & interview discussion are available to premium members.

Log in Create a free account