Check whether Pair with a Sum exists. Solved!
Pradip Dharam
Python Developer, Data Engineer, pySpark, ML (NLP) Product's Features Development & Refactoring, PR Reviews, git, MongoDB, Familiar with Airflow, Docker. Prototyping, Pandas, Numpy, SQL, sklearn. Novice to CICD.
Given an array?arr?of?N?integers, and an integer?K, Determine whether or not there exist two elements in?arr?whose sum is exactly?K.
# n = 6
# k = 16
# arr = [1, 4, 45, 6, 10, 8]
nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]
arr = list(map(int, input().split()))
i = 0
j = n - 1
exists = False
arr = sorted(arr)
while i < j:
if arr[i] + arr[j] > k:
j = j - 1
elif arr[i] + arr[j] < k:
i = i + 1
else:
exists = True
break
if exists:
print("Yes")
else:
print("No")
# Test Case 1 Accepted
# Test Case 2 Accepted
# Test Case 3 Accepted
# Test Case 4 Accepted
# Test Case 5 Accepted
# Test Case 6 Accepted
# Test Case 7 Accepted
# Test Case 8 Accepted
# Test Case 9 Accepted
# Test Case 10 Accepted