-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
30 lines (29 loc) · 842 Bytes
/
Program.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
using System;
namespace ThekStrongestValuesInAnArray
{
class Program
{
static void Main(string[] args)
{
int[] arr = {-7, 22, 17, 3};
int k = 2;
Console.WriteLine(GetStrongest(arr, k));
}
static int[] GetStrongest(int[] arr, int k)
{
var res = new int[k];
Array.Sort(arr);
int li = 0, hi = arr.Length - 1;
int median = arr.Length % 2 == 0 ? arr[arr.Length / 2 - 1] : arr[arr.Length / 2];
int point = 0;
while (point < k)
{
if (Math.Abs(arr[li] - median) > Math.Abs(arr[hi] - median))
res[point++] = arr[li++];
else
res[point++] = arr[hi--];
}
return res;
}
}
}