Skip to main content

TCS

TCS OA-1 2024

Problem Description

Given an array of integer nums and an integer k, return the total number of subarrays whose sum equals k.
A subarray is a contiguous non-empty sequence of elements within an array.  

Input Format

  • The first line contains an integer N, representing the length of the array.
  • The second line contains N space-separated integers.
  • The third line contains an integer k.

Output Format

Print the total number of subarrays whose sum equals k.

Constraints

  •  1 ≤ arr.size() ≤ 10⁴
  • -10³ ≤ arr[i] ≤ 10³
  • -10⁷ ≤ k ≤ 10⁷

Example

Sample Input:

7
[0, 1, 2, 3, 4, 5, 6]
9

Sample Output:

2

Explanation:

There are 2 subarrays whose sum is 9 ie [2, 3, 4] and [4, 5].