Last active
February 26, 2021 09:53
-
-
Save lifeparticle/c0006aaa53c20b6ac5e19fa86d75dae5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int firstOccurrence (int values[], int target) { | |
int left = 0; | |
int mid = 0; | |
int right = values.length - 1; | |
int pos = -1; | |
while (left <= right) { | |
mid = (left + right) / 2; | |
if (target == values[mid]) { | |
pos = mid; | |
right = mid - 1; | |
} else if(target > values[mid]) { | |
left = mid + 1; | |
} else { | |
right = mid - 1; | |
} | |
} | |
return pos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment