-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDivisbleSumPairs.cs
57 lines (43 loc) · 1.45 KB
/
DivisbleSumPairs.cs
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
54
55
56
57
// You are given an array of integers, , and a positive integer, . Find and print the number of pairs where and + is divisible by .
// Input Format
// The first line contains space-separated integers, and .
// The second line contains space-separated integers describing the values of .
// Constraints
// Output Format
// Print the number of pairs where and + is evenly divisible by .
// Sample Input
// 6 3
// 1 3 2 6 1 2
// Sample Output
// 5
// Explanation
// Here are the valid pairs when :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static int divisibleSumPairs(int n, int k, int[] ar) {
// Complete this function
var counter = 0;
var divisibleSumPairCount = 0;
while (counter < n) {
for (var i=counter+1; i<n; i++) {
if (((int)ar[counter] + (int)ar[i]) % k ==0 ) {
divisibleSumPairCount++;
}
}
counter++;
}
return divisibleSumPairCount;
}
static void Main(String[] args) {
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int k = Convert.ToInt32(tokens_n[1]);
string[] ar_temp = Console.ReadLine().Split(' ');
int[] ar = Array.ConvertAll(ar_temp,Int32.Parse);
int result = divisibleSumPairs(n, k, ar);
Console.WriteLine(result);
}
}