Divisible Sum Pairs [Hacker Rank]

Kim Hayeon·2023년 4월 18일
0

Algorithm Study

목록 보기
8/37
post-thumbnail

Question

Given an array of integers and a positive integer k , determine the number of (i,j) pairs where and ar[i] + ar[j] is divisible by k.

Function Description

Complete the divisibleSumPairs function in the editor below.

divisibleSumPairs has the following parameter(s):

  • int n: the length of array
  • int ar[n]: an array of integers
  • int k: the integer divisor

Returns

  • int: the number of pairs

Input Format

The first line contains 2 space-separated integers n, and k.
The second line contains n space-separated integers, each a value of arr[i].

Constraints

  • 2 <= n <= 100
  • 1 <= k <= 100
  • 1 <= ar[i] <= 100

Sample Input

STDIN Function


6 3 n = 6, k = 3
1 3 2 6 1 2 ar = [1, 3, 2, 6, 1, 2]

Sample Output

5


풀이


def divisibleSumPairs(n, k, ar):
    answer = 0
    for i in range(n-1):
        for j in range(i+1,n):
            if (ar[i] + ar[j]) % k == 0:
                 answer += 1
    return answer
    

범위가 작아서 이중 for문으로 모든 경우를 확인했다!

profile
우리는 무엇이든 될 수 있어

0개의 댓글