Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Search Insert Position

Easy Acceptance 48.03% Points 20

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.

Constraints
  • 1 <= arr.size() <= 10^5
  • 1 <= arr[i] <= 10^6
  • 1 <= k <= 10^6
Hints
  • Expected Time Complexity: O(log n)
  • Expected Auxiliary Space: O(log n)
Topics
Array Sorting Algorithms
Companies
Oracle TCS Accenture Infosys Cognizant Wipro
Added 22 Feb 2025 48.03% acceptance
Ln 1, Col 1Sign in to save your code
5
11 22 33 44 55
445
-1