#K84197. Pair Sum Query
Pair Sum Query
Pair Sum Query
You are given an array of positive integers and a series of queries. Each query is specified by three integers \(l\), \(r\), and \(X\), which represent a subarray from index \(l\) to \(r\) (inclusive) and a target sum \(X\). Your task is to determine whether there exists a pair of distinct indices within the given subarray such that the sum of the two corresponding elements equals \(X\).
Formally, for each query, check if there exist indices \(i\) and \(j\) (with \(l \le i < j \le r\)) such that \(arr[i] + arr[j] = X\). All indices are 1-indexed.
You must read the input from standard input and write the results to standard output.
inputFormat
The input consists of multiple lines:
- The first line contains an integer \(N\) representing the number of elements in the array.
- The second line contains \(N\) positive integers separated by spaces, representing the array elements.
- The third line contains an integer \(Q\) denoting the number of queries.
- Each of the next \(Q\) lines contains three integers \(l\), \(r\), and \(X\) separated by spaces.
outputFormat
For each query, print a single line containing YES
if there exists a valid pair in the subarray whose sum equals \(X\); otherwise, print NO
.
5
1 2 3 4 5
3
1 4 5
2 5 7
1 5 10
YES
YES
NO
</p>