-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautiful-triplets.cs
26 lines (26 loc) · 1021 Bytes
/
beautiful-triplets.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
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int d = Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
Dictionary<int, int> dic = new Dictionary<int, int>();
for(int x = 0, l = a.Length; x < l; x++)
{
dic.Add(a[x], x);
}
int count = 0;
foreach(KeyValuePair<int, int> kvp in dic)
{
int newKey = kvp.Key + d;
count += ( dic.ContainsKey(newKey) && dic[newKey] > kvp.Value &&
dic.ContainsKey(newKey + d) && dic[newKey + d] > dic[newKey]) ? 1 : 0;
}
Console.WriteLine(count.ToString());
}
}