Find Pair with Sum
EasyPractice library · +10 XPtwo pointersarraytwo sum
Given a sorted array of distinct integers and a target sum, determine if there exists a pair of elements in the array whose sum is exactly equal to the target sum. If such a pair exists, print "YES". Otherwise, print "NO".
Input
The first line contains two integers N and S, the size of the array and the target sum. The second line contains N space-separated integers, representing the sorted array.
Output
Print "YES" if a pair with the target sum exists, and "NO" otherwise.
Constraints
1 <= N <= 1000 -10^9 <= arr[i] <= 10^9 arr is sorted in non-decreasing order. arr contains distinct integers.
Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests
Example 1
Input
5 10 1 2 3 4 5
Output
NO
For array [1,2,3,4,5] and target sum 7, the pair (3,4) sums to 7.
Example 2
Input
5 10 1 2 3 4 5
Output
NO
For array [1,2,3,4,5] and target sum 10, no pair of distinct elements sums to 10.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 10 1 2 3 4 5
Example 2 input
5 10 1 2 3 4 5