Given a sorted array arr[] containing unique elements and an integer k, your task is to find the index (0-based) of the largest element in the array that is less than or equal to k. This element is referred to as the "floor" of k. If no such element exists, return -1.
Example 1
Input: arr[] = [3, 6, 8, 10, 15, 18], k = 7
Output: 1
Explanation: The largest number less than or equal to 7 is 6, and its index is 1.
Example 2
Input: arr[] = [1, 2, 8, 10, 11, 12, 19], k = 20
Output: 6
Explanation: The largest number less than or equal to 20 is 19, and its index is 6.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
3 6 8 10 15 18 7
1
1 2 8 10 11 12 19 20
6