#K92692. Maximum Subarray Length with Target Sum
Maximum Subarray Length with Target Sum
Maximum Subarray Length with Target Sum
Given an array of integers and an integer target, your task is to find the maximum length of a contiguous subarray whose sum equals the target. If no such subarray exists, output 0.
You are required to read input from the standard input (stdin) and output the result to the standard output (stdout).
Note: A contiguous subarray is a sequence of elements from the array that are adjacent to each other.
Formula: We are looking for the maximum length L such that for some indices i and j with 0 ≤ i ≤ j < n,
\(\sum_{k=i}^{j} nums[k] = target\)
inputFormat
The input consists of two lines.
- The first line contains two integers
n
andtarget
wheren
is the number of elements in the array. - The second line contains
n
space-separated integers representing the arraynums
.
outputFormat
Output a single integer, the maximum length of a contiguous subarray that sums to target
. If no such subarray exists, output 0.
5 3
1 -1 5 -2 3
4
</p>