-
Notifications
You must be signed in to change notification settings - Fork 0
/
58-HackerRankTest-BinarySearch.cs
50 lines (45 loc) · 1.23 KB
/
58-HackerRankTest-BinarySearch.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HackerRank
{
internal class HackerRankSampleTest_Solution
{
public static string findNumber(int[] array, int k)
{
int[] arr = array.ToArray();
Array.Sort(arr);
int min = 0;
int max = arr.Length-1;
while(min <= max)
{
int target = Convert.ToInt32(Math.Floor(Convert.ToDecimal((min + max) / 2)));
if (arr[target] == k)
{
return "YES";
}
else if (arr[target] < k)
{
min = target + 1;
}
else
{
max = target - 1;
}
}
return "NO";
}
}
internal class HackerRankSampleTest_Result
{
public static void Input()
{
int[] array = new int[] {1,2,3,4,5,6 };
int k = 4;
string result = HackerRankSampleTest_Solution.findNumber(array,k);
Console.WriteLine(result);
}
}
}