forked from CodeSadhu/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivisibleSumPairs.kt
53 lines (42 loc) · 1.3 KB
/
DivisibleSumPairs.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* You are given an array of n integers, ar = [ar[0], ar[1],...,ar[n-1]], and a positive integer, k.
* Find and print the number of (i, j) pairs where i<j and ar[i] + ar[j] is divisible by k.
*/
import java.io.*
import java.math.*
import java.security.*
import java.text.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.regex.*
import java.util.stream.*
import kotlin.collections.*
import kotlin.comparisons.*
import kotlin.io.*
import kotlin.jvm.*
import kotlin.jvm.functions.*
import kotlin.jvm.internal.*
import kotlin.ranges.*
import kotlin.sequences.*
import kotlin.text.*
// Complete the divisibleSumPairs function below.
fun divisibleSumPairs(n: Int, k: Int, ar: Array<Int>): Int {
var count: Int = 0
for (i in 0..n-2) {
for (j in i+1..n-1) {
val total = ar[i] + ar[j]
if (total % k == 0)
count++
}
}
return count
}
fun main(args: Array<String>) {
val scan = Scanner(System.`in`)
val nk = scan.nextLine().split(" ")
val n = nk[0].trim().toInt()
val k = nk[1].trim().toInt()
val ar = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()
val result = divisibleSumPairs(n, k, ar)
println(result)
}