Skip to content
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

added question and solution on floor of a number in sorted array #385

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Task 1/Floor of A Number In Sorted Array/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Find the floor of a number in a sorted array .
28 changes: 28 additions & 0 deletions Task 1/Floor of A Number In Sorted Array/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class FloorOfANumber {

public static void main(String[] args) {
int[] arr = {9, 11, 19, 22,23,24};
int target = 20;
int ans = floor(arr, target);
System.out.println(ans);
}

static int floor(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;

while(start <= end) {
// finding the middle element
int mid = (start + end) / 2;
if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
// ans is found
return mid;
}
}
return end;
}
}