#K95252. Subarray Sum Finder

    ID: 38822 Type: Default 1000ms 256MiB

Subarray Sum Finder

Subarray Sum Finder

You are given an array of integers and a target sum. Your task is to determine whether there exists a contiguous subarray whose sum is equal to the given target.

Formally, let the array be \(a_1, a_2, \ldots, a_n\). We want to find indices \(i\) and \(j\) (with \(1 \le i \le j \le n\)) such that:

\[ a_i + a_{i+1} + \cdots + a_j = target \]

An efficient approach uses prefix sums. Define \(S_k = \sum_{i=1}^{k} a_i\) (with \(S_0 = 0\)). Then for any indices \(i \leq j\), the subarray sum \(a_i + \cdots + a_j = S_j - S_{i-1}\). The condition becomes:

\(S_j - S_{i-1} = target\)

Implement a function that checks whether such a subarray exists.

inputFormat

The input is given from stdin and has the following format:

  • The first line contains an integer \(n\) representing the number of elements in the array.
  • The second line contains \(n\) space-separated integers, the elements of the array.
  • The third line contains an integer representing the target sum.

outputFormat

Output to stdout a single line containing True if there exists at least one contiguous subarray with the given sum, or False otherwise.

## sample
6
1 4 20 3 10 5
33
True