FIND 0s AND 1s INTERFACE POINT (BINARY SEARCH TECHNIQUE)
namespace Find_0_s_and_1_s_interface
{
class Program
{
static int FindInterface(int[] arr)
{
int l = 0;
int h = arr.Length - 1;
int mid = 0;
while (l < h)
{
mid = (l + h) / 2;
if (arr[mid] == 0 && arr[mid + 1] == 1) return mid;
if (arr[mid] == 1 && arr[mid - 1] == 0) return mid-1;
else if (arr[mid] == 0) l = mid+1;
else h = mid-1;
}
return -1;
}
static void Main(string[] args)
{
int[] A = { 0,0,0,1,1,1,1 };
Console.WriteLine(FindInterface(A));
}
}
}
No comments:
Post a Comment