Skip to content

Added 2 problems to Leetcode: #34, #103 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Added 2 problems to Leetcode: #34, #103 #6

wants to merge 2 commits into from

Conversation

mohitkhosla01
Copy link

@mohitkhosla01 mohitkhosla01 commented Oct 7, 2019

#34 - Find First and Last Position of Element in Sorted Array

#103 - Find First and Last Position of Element in Sorted Array

@mohitkhosla01 mohitkhosla01 changed the title Added new problem to Leetcode Added 2 problems to Leetcode: #34, #103 Oct 7, 2019
Comment on lines +70 to +129
public static int[] searchRange(int[] nums, int target) {

int[] range = {-1, -1};

if(nums == null) {
return range;
}

int b = 0, e = nums.length - 1;
int b1 = 0, e1 = nums.length - 1;
boolean leftFound = false;

while(b <= e) {
int mid = (b + e)/2;

if(nums[mid] != target) {
if(nums[mid] > target) {
e = mid - 1;
e1 = mid - 1;
}
else {
b = mid + 1;
b1 = mid + 1;
}
}
else {
if(range[0] == -1) {
range[0] = mid;
}
if(range[1] == -1) {
range[1] = mid;
}

if(!leftFound) {
if(mid == 0 || nums[mid-1] != nums[mid]) {
leftFound = true;
range[0] = mid;
b = b1;
e = e1;
}
else {
b = b1;
e = mid;
}
}
else {
if(mid == nums.length - 1 || nums[mid+1] != nums[mid]) {
range[1] = mid;
break;
}
else {
b = mid + 1;
e = e1;
}
}
}
}

return range;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, it would be great if you can add a recursive version of this as well.
Something like this, Optimization in this is welcome

   static int left, right;
    public static int[] searchRange(int[] nums, int target) {
        left = -1;
        right = -1;
        searchRange(nums, 0,nums.length-1, target);
        int [] result = new int[]{left, right};
        return result;  
    }
    
    private static void searchRange(int []nums, int low, int high, int target) {
        if(low <= high) {
            
            int mid = low + (high - low) / 2;
            
            if(nums[mid] < target) {
                searchRange(nums, mid+1, high, target);
            } else if (nums[mid] > target) {
                searchRange(nums, low, mid-1, target);
            } else { // Data Match
                if(mid == 0 || (left == -1 && nums[mid-1] != target)) {
                    left = mid;
                }
                if(mid == nums.length - 1 || (right == -1 && nums[mid+1] != target)) {
                    right = mid;
                }
                
                if(left == -1) {
                    searchRange(nums, low, mid-1, target);
                } 
                if(right == -1) {
                    searchRange(nums, mid+1, high, target);
                }
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants