본문 바로가기
Programming Test/문제풀이

[문제 풀이 18] 코드 트리 - 정수 두 개의 합

by muns91 2024. 4. 18.
정수 두 개의 합

 

  • 문제 유형 : HashMap
  • 사용언어 : Python
  • 난이도 : 실버 4
  • 출처 : 코드 트리

https://www.codetree.ai/training-field/search/problems/sum-of-two-integers?&utm_source=clipboard&utm_medium=text

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

n = int(input())
m = int(input())

arr = list(map(int, input().split()))

def count_sort(arr, m):
    hash_table = [0]*(m+1)

    for num in arr:
        hash_table[num] = 1

    return hash_table


hash_table = count_sort(arr, m)

cnt = 0
for num in arr:
    complement = m-num

    if (complement != num and complement>=0 and complement<=m and hash_table[complement]==1):
        cnt +=1
        
print(int(cnt/2))