-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathKDifference.cs
41 lines (34 loc) · 1.04 KB
/
KDifference.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
using System;
using System.Collections.Generic;
namespace PracticeQuestionsSharp.Exercises.Numbers
{
//Given an array of n distinct numbers, find all pairs that have a difference of k.
public static class KDifference
{
//Brute force solution
public static int FindKDifferencePairs1(int[] arr, int k)
{
int count = 0;
for (int i = 0; i < arr.Length - 1; ++i)
{
for (int j = i + 1; j < arr.Length; ++j)
{
if (Math.Abs(arr[i] - arr[j]) == k) count++;
}
}
return count;
}
//Hash solution
public static int FindKDifferencePairs2(int[] arr, int k)
{
int count = 0;
Dictionary<int, int> dict = new Dictionary<int, int>();
foreach (int i in arr)
dict.Add(i, i);
foreach (int i in arr)
if (dict.ContainsKey(i + k))
count++;
return count;
}
}
}