Coding Global Background
Coding Global

Array Index & Element Equality

Archived 3 years ago
1
6 messages
2 members
Created 3 years ago
Updated 3 years ago
Open in Discord
H
horang
Given a sorted array arr of distinct integers, write a function indexEqualsValueSearch that returns the lowest index i for which arr[i] == i. Return -1 if there is no such index. Analyze the time and space complexities of your solution and explain its correctness.

: arr = [-8,0,2,5]
output: 2 # since arr[2] == 2

input: arr = [-1,0,3,6]
output: -1 # since no index in arr satisfies arr[i] == i.


[time limit] 5000ms
[input] array.integer arr1 ≤ arr.length ≤ 100
[output] integer
Array Index & Element Equality

Replies (6)