Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Search Insert Position

Easy Acceptance 48.03% Points 20.00

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
Oracle TCS Accenture Infosys Cognizant + 1 more
Topics
Array Sorting Algorithms
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions