less than 1 minute read

Divisible Sum Pairs (Hacker Rank)

You are given an array of integers, , and a positive integer, . Find and print the number of pairs where and + is divisible by .

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int divisibleSumPairs(int n, int k, int[] ar) {
        int sum = 0, result = 0;
        for(int i = 0; i < n; i++){
            for(int j = (i+1); j < n ; j++){
               sum = ar[i] + ar[j];
               if( sum%k ==0){
                   result++;
               }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int result = divisibleSumPairs(n, k, ar);
        System.out.println(result);
    }
}
행운의 77문제 프로젝트
  행운의 77문제 프로젝트는 한 달동안 알고리즘 문제 77개를 푸는 프로젝트입니다.