@@ -0,0 +1,1454 @@
+package array.problems;%0D%0A%0D%0Apublic class Search %7B%0D%0A%09%0D%0A%09/**%0D%0A%09 * @author    mahesh.gaonkar%0D%0A%09 * @since     21-12-2016%0D%0A%09 * @param     nums: The integer array.%0D%0A%09 * @param     target: Target to find.%0D%0A%09 * @return    position: The first position of target. Position starts from 0. %0D%0A%09 * @exception NullPointerException if nums == null%0D%0A%09 * @print     ex.getMessage(), If @exception found%0D%0A%09 */%0D%0A%09public static int binarySearch(final int%5B%5D nums,  final int target) %7B%0D%0A%09%09 int position = -1;%0D%0A%09%09try%7B%0D%0A%09%09%09%0D%0A%09%09%09if(nums == null)%7B%0D%0A%09%09%09%09throw new NullPointerException();%0D%0A%09%09%09%7D%0D%0A%09%09%09%0D%0A%09%09%09int start = 0;%0D%0A%09        int end = nums.length - 1;%0D%0A%09         %0D%0A%09        while(start %3C= end )%7B%0D%0A%09            int mid = (start + end) / 2;%0D%0A%09            if(nums%5Bmid%5D == target)%7B%0D%0A%09                position = mid;%0D%0A%09                break;%0D%0A%09            %7D%0D%0A%09            else if(nums%5Bmid%5D %3C target)%0D%0A%09                start = mid + 1;%0D%0A%09            else%0D%0A%09                end = mid - 1;%0D%0A%09        %7D%0D%0A%09       %0D%0A%09     // Only if the target number is repeat more than one%0D%0A%09        if(position != -1)%7B%0D%0A%09            while(position %3E= 0 && position %3C nums.length && nums%5Bposition%5D == nums%5Bposition - 1%5D)%7B%0D%0A%09                position = position - 1;%0D%0A%09            %7D%0D%0A%09        %7D%0D%0A%09       // return position;%0D%0A%09%09%7Dcatch(NullPointerException ex)%7B%0D%0A%09%09%09System.out.println(ex.getMessage());%0D%0A%09%09%7D%0D%0A%09%09catch(Exception ex)%7B%0D%0A%09%09%09System.out.println(ex.getMessage());%0D%0A%09%09%7D%0D%0A%09%09 return position;%0D%0A    %7D%0D%0A%7D%0D%0A

First Position of Target