Question bank › Arrays
Dsa Arrays Easy

Subarray with Given Sum

Given an integer array and a target sum, find the length of the longest contiguous subarray that sums up to the target sum. If no such subarray exists, return 0. Input Format: The first line contains two integers, n (the size of the array) and target (the target sum). The second line contains n space-separated integers, which are the elements of the array. Output Format: Output a single integer representing the length of the longest subarray that sums to the target. Example: Input: 5 8 2 3 1 4 2 Output: 3 In the example above, the subarray [3, 1, 4] sums to 8 and has a length of 3.

Key concepts

arrayssubarrayssums

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