1. K로 나눠지는지를 볼때 두 수의 합은 두 수의 나머지의 합을 봐도 무방함
2. 두 수의 합이므로 K // 2 의 범위에서 떨어지지 않는 수를 찾고, count가 더 많은 수를 더해줌
3. 짝수인 경우엔 하나만 고려해서 더해줌

def nonDivisibleSubset(k, s):
    # Write your code here
    r = [0] * k
    cnt = 0
    
    for i in s:
        r[i % k] += 1
            
    for i in range(k//2 + 1):
        if i == 0 or k == i * 2:
            cnt += bool(r[i])
        else:
            cnt += max(r[i], r[k - i])
    
    return cnt

상세 설명 ↓
https://cs.stackexchange.com/questions/57873/maximum-subset-pairwise-not-divisible-by-k

 

Maximum subset pairwise not divisible by $K$

I have a set of numbers, and want to calculate the maximum subset such that the sum of any two of it's elements is not divisible by an integer $K$. I tried to solve this problem, but I have found the

cs.stackexchange.com

 

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

Jumping on the Clouds  (0) 2022.08.23
[HackerRank-python] Repeated String  (0) 2022.08.22
[HackerRank-python] Cut the sticks  (0) 2022.08.11
[HackerRank-python] Library Fine  (0) 2022.08.09
[HackerRank-python] Sherlock and Squares  (0) 2022.08.09