#K50452. Subarray with Given Sum

    ID: 28867 Type: Default 1000ms 256MiB

Subarray with Given Sum

Subarray with Given Sum

Given an array of integers \(arr\) and a target integer \(T\), determine whether there exists a contiguous subarray whose sum equals \(T\). A subarray is defined as a consecutive segment of the array. If such a subarray exists, output "YES"; otherwise, output "NO".

For example, if \(arr = [1, 2, 3, 4, 5]\) and \(T = 9\), one valid subarray is \([2,3,4]\), and the answer is "YES".

Note: The array may include negative numbers. The problem requires an efficient solution that handles all cases.

inputFormat

The input is given via stdin and consists of three lines:

  • The first line contains an integer \(n\) representing the number of elements in the array.
  • The second line contains \(n\) space-separated integers which constitute the array \(arr\). (If \(n = 0\), this line may be empty.)
  • The third line contains an integer \(T\), the target sum.

For example:
5 1 2 3 4 5 9

outputFormat

Output a single line to stdout containing either "YES" if there exists at least one contiguous subarray that sums to \(T\), or "NO" if there is no such subarray.

## sample
5
1 2 3 4 5
9
YES