Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@

public class Problem1 {

public static void main(String[] args) {

//find 4 in array = [1,2,3,5,6,7] using binary search
int[] arr = {1, 2, 3, 5, 6, 7};
int target = 4;
int result = binarySearch(arr, target);
if (result == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index: " + result);
}

}
public return int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == mid +1){
left = mid +1;
} else {
right = mid -1;
}
}

return left + 1;
}
}