You are given an array of integers, , and a positive integer, . Find and print the number of pairs where and + is divisible by .
For example, and . Our three pairs meeting the criteria are and .
Question from hackerrank.
Solution in C# -
class Solution {
static int divisibleSumPairs(int n, int k, int[] ar) {
int div = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if((ar[i]+ar[j])%k == 0)
div++;
}
}
return div;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] nk = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(nk[0]);
int k = Convert.ToInt32(nk[1]);
int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp));
int result = divisibleSumPairs(n, k, ar);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}
Post a Comment