You are given a sorted array arr[] of distinct integers in non-decreasing order and an integer k, return true if k is present in the array, otherwise false.
Note: If multiple occurrences are there, please return the smallest index.
Examples
Example 1
Example 1
Input: arr[] = [1, 2, 3, 4, 5], k = 4
Output: 3
Explanation: 4 appears at index 3.
Example 2
Example 2
Input: arr[] = [11, 22, 33, 44, 55], k = 445
Output: -1
Explanation: 445 is not present.
Hints
Hint 1
Expected Time Complexity: O(log n)
Hint 2
Expected Auxiliary Space: O(log n)
Constraints
1 <= arr.size() <= 10^5
1 <= arr[i] <= 10^6
1 <= k <= 10^6
Companies
OracleTCSAccentureInfosysCognizant+ 1 more
Topics
ArraySorting Algorithms
Track your submissions
Please log in to review your progress and explore code submissions from other participants.
Unlock the full solution
Please log in to access detailed answers and explanations.
Join the discussion
Please log in to join conversations with other participants.
Solution.csC#JavaPythonC++Javascript
1
2
3
4
5
6
7
8
9
10
Unlock the code editor
Sign in to write, run, and submit your solution against the full test suite.