Skip to content

Commit

Permalink
Merge pull request #385 from itsAftabAlam/main
Browse files Browse the repository at this point in the history
added question and solution on floor of a number in sorted array
  • Loading branch information
nimishbongale committed Oct 27, 2021
2 parents d676698 + 3bad764 commit b0e00ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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;
}
}

0 comments on commit b0e00ce

Please sign in to comment.