Check whether Pair with a Sum exists. Solved!
Generated on LinkedIn

Check whether Pair with a Sum exists. Solved!

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        

要查看或添加评论,请登录

Pradip Dharam的更多文章

社区洞察

其他会员也浏览了