Dsa
Prefix Sum
Medium
Subarray Sum Occurrences
You are given an integer array `nums` and an integer `k`. Your task is to count the number of continuous subarrays whose sum equals to `k`. Please write a program that reads the array and the value of `k`, then outputs the count of these subarrays.
Input format:
- The first line contains an integer `n`, the number of elements in the array.
- The second line contains `n` space-separated integers representing the elements of the array `nums`.
- The third line contains the integer `k`.
Output format:
- Output a single integer, the count of continuous subarrays that sum to `k`.
Example:
Input:
5
1 1 1 1 1
2
Output:
4
Key concepts
prefix_sumsubarraycounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now