forked from jhflorey/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAngrychildren.cs
executable file
·53 lines (42 loc) · 1.25 KB
/
Angrychildren.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AngryChildren
{
class Solution
{
private static int[] mySet;
static string AngryChildren(int k)
{
Array.Sort(mySet);
int currentMin = 0;
int currentMax = k - 1;
int unfairness = Int32.MaxValue;
int currentUnfairness = 0;
while (currentMax < mySet.Length)
{
currentUnfairness = Math.Abs(mySet[currentMax] - mySet[currentMin]);
if (currentUnfairness < unfairness)
unfairness = currentUnfairness;
++currentMax;
++currentMin;
}
return unfairness.ToString();
}
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine().Trim());
mySet = new int[n];
int k = Convert.ToInt32(Console.ReadLine().Trim());
while (n > 0)
{
n--;
int item = Convert.ToInt32(Console.ReadLine());
mySet[n] = item;
}
Console.WriteLine(AngryChildren(k));
}
}
}