Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

1stoccurence

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static int Find1stOccurence_1(int [] arr, int num)
        {
            if(arr == null)
            {
                throw new ArgumentNullException("arr");
            }
            
            int l = 0;
            int h = arr.Length-1;
            
            while(l <= h)
            {
                int mid = (l+h)/2;
                if(arr[mid] == num)
                {
                    if(mid >= 1)
                    {
                        if(arr[mid-1] != num)
                        {
                            return mid;
                        }
                        else 
                        {
                            h = mid - 1;
                        }
                    }
                    else 
                    {
                        return mid;
                    }
                }
                else if (arr[mid] > num)
                {
                    h = mid - 1;
                }
                else if(arr[mid] < num)
                {
                    l = mid + 1;
                }
            }
            
            return -1;
        }
        
        
        public static int Find1stOccurence(int [] arr, int num)
        {
            if(arr == null)
            {
                throw new ArgumentNullException("arr");
            }
            
            int l = 0;
            int h = arr.Length-1;
            
            while(l <= h)
            {
                int mid = (l+h)/2;
                if(arr[mid] == num)
                {
                    if((mid-1) >= 0 && arr[mid-1] == num)
                    {
                        int k = mid-1;
                        while(k >= 0 && arr[mid] == arr[k])
                        {
                            k--;
                        }
                        
                        return k;
                    }
                    else
                    {
                        return mid;
                    }
                }
                
                if (arr[mid] > num)
                {
                    h = mid-1;
                }
                
                if(arr[mid] < num)
                {
                    l = mid+1;
                }
            }
            
            return -1;
        }
        
        public static void Main(string[] args)
        {  
            int [] arr = {1, 2, 2, 3, 3, 4, 4, 5, 5, 6};
            //int [] arr = {2, 2};
            Console.WriteLine(Find1stOccurence_1(arr, 7));
        }
    }
}
 run  | edit  | history  | help 0