Question bank › Prefix Sum
Dsa Prefix Sum Medium

Count Subarrays with Fixed Sum

You are given an integer array `nums` and an integer `target`. Your task is to count the number of contiguous subarrays whose sum equals `target`. Write a program to read the array and the target, and then print 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 values in the array `nums`. - The third line contains an integer `target`. Output format: - Output a single integer that indicates the number of subarrays that sum to `target`. Example: Input: 4 1 2 3 4 3 Output: 2

Key concepts

prefix_sumsubarrayhash_map

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
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.