Skip to content

Commit

Permalink
create solution file on the question mentioned in this folder
Browse files Browse the repository at this point in the history
  • Loading branch information
itsAftabAlam committed Oct 16, 2021
1 parent 4d09fb6 commit 3bad764
Showing 1 changed file with 28 additions and 0 deletions.
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 3bad764

Please sign in to comment.