Untitled

 avatar
unknown
python
2 months ago
579 B
7
Indexable
n = int(input()) 
k = int(input()) 
arr = [] 
for i in range(n):
    arr.append(int(input())) 
print(arr) 
def brute_force(arr,k):
    count = 0 
    for i in range(len(arr)):
        for j in range(i + 1,len(arr)):
            if arr[i] + arr[j] == k:
                count = count + 1 
    return count 
from collections import defaultdict 
def optimal_solve(arr,k):
    mp = defaultdict(int) 
    for i in range(len(arr)):
        comp = k - arr[i] 
        if comp in mp:
            count = count + mp[comp] 
        mp[arr[i]] += 1 
    return count 
Editor is loading...
Leave a Comment