Skip to content

Commit aeb5c85

Browse files
committed
Solution to find the range of index for a given Number in a Sorted Array
1 parent 907bd93 commit aeb5c85

3 files changed

Lines changed: 71 additions & 15 deletions

File tree

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package BinaySearch;
22

3-
import java.util.Arrays;
4-
5-
public class FindNumber {
3+
public class BinarySearch {
64
public static void main(String[] args) {
75

86
int[] array = {1,2,3,4,5,6,77,88,99,102,105,115,167};
@@ -17,16 +15,17 @@ static int binarySearch(int[] array, int target){
1715

1816
while(startIndex <= endIndex){
1917
//Find Middle Element
20-
int middle = (startIndex+endIndex)/2;
18+
int middle = startIndex + (endIndex - startIndex)/2;
19+
2120
if (target == array[middle]){
2221
return middle;
2322
}else {
24-
if (target > array[middle]){
25-
startIndex = middle+1;
26-
System.out.println("Start Index Incremented");
27-
}else{
23+
if (target < array[middle]){
2824
endIndex = middle-1;
29-
System.out.println("End Index Decremented");
25+
// System.out.println("Start Index Incremented");
26+
}else{
27+
startIndex = middle+1;
28+
// System.out.println("End Index Decremented");
3029
}
3130
}
3231
}

‎src/BinaySearch/LeetCode744.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ static char nextGreatestLetter(char[] letters, char target) {
1515
//Find Middle Element
1616
int middle = startIndex + (endIndex - startIndex)/2;
1717

18-
if (target < letters[middle]){
19-
endIndex = middle-1;
20-
21-
}else{
22-
startIndex = middle+1;
18+
if (target > letters[middle]){
19+
startIndex = middle+1;
20+
System.out.println("Start Index Incremented");
21+
}else{
22+
endIndex = middle-1;
23+
System.out.println("End Index Decremented");
24+
}
2325
}
24-
}
2526
return letters[startIndex % letters.length];
2627
}
2728
}

‎src/BinaySearch/SearchRange.java‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package BinaySearch;
2+
3+
import java.util.Arrays;
4+
5+
public class SearchRange {
6+
public static void main(String[] args) {
7+
8+
int[] array = {5,7,7,8,8,10};
9+
10+
int[] arrayWithAnswers = searchRange(array,6);
11+
System.out.println(Arrays.toString(arrayWithAnswers));
12+
}
13+
14+
static int[] searchRange(int[] nums, int target) {
15+
16+
int[] ans = {-1,-1};
17+
18+
ans[0] = getIndex(nums, target, true);
19+
if (ans[-1] != -1){
20+
ans[1] = getIndex(nums, target, false);
21+
}
22+
23+
return ans;
24+
}
25+
26+
//This function returns the index of the target number
27+
static int getIndex(int[] array, int target, boolean findStartIndex){
28+
29+
int answer = -1;
30+
31+
int startIndex = 0;
32+
int endIndex = array.length -1;
33+
34+
while(startIndex <= endIndex){
35+
//Find Middle Element
36+
int middle = startIndex + (endIndex - startIndex)/2;
37+
38+
if (target < array[middle]){
39+
endIndex = middle-1;
40+
// System.out.println("Start Index Incremented");
41+
}else if(target > array[middle]){
42+
startIndex = middle+1;
43+
// System.out.println("End Index Decremented");
44+
}else{
45+
answer = middle;
46+
if (findStartIndex){
47+
endIndex = middle -1;
48+
}else {
49+
startIndex = middle + 1;
50+
}
51+
}
52+
}
53+
54+
return answer;
55+
}
56+
}

0 commit comments

Comments
 (0)