Skip to content
This repository was archived by the owner on Nov 11, 2021. It is now read-only.
This repository was archived by the owner on Nov 11, 2021. It is now read-only.

Binary search solution problem #3

Description

@juandgaines

For the binary search solution (), the binary search algorithm is not working finding the first and the last numbers.
I propose this approach.

`public static int findTarget(int[] arr, int target) {
if (arr.length == 0) return -1;

    int start = 0, end = arr.length;
    while (start<=end) {
        int mid = (start+end)/2;
        if(arr[mid]==target)
            return mid;
        else if(arr[mid]<target){
            start=mid+1;
        }
        else {
            end=mid-1;
        }
    }
    return -1;
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions