Array Index Element Equality LeetCode

Given an array of sorted distinct integers named arr, write a function that returns an index i in arr for which arr[i] = i or -1 if no such index exists.

Implement the most efficient solution possible, prove the correctness of your solution and analyze its runtime complexity (in terms of N - the length of arr).

Example 1:
  • Given arr = [-8,0,2,5] the function returns 2, since arr[2] = 2

Example 2:
  • Given arr = [-1,0,3,6] the function returns -1, since no index in arr satisfies arr[i] = i
This is an easy array search problem and is also popular in GeeksForGeeks and in different public forums such as StackOverFlow - Link 1 and Link 2 A collection of hundreds of interview questions and solutions are available in our blog at Interview Question Solutions

Solution:
 

 

No comments:

Post a Comment