6 kyu

Element equals its index

504 of 1,998sgerodes

Description:

Given a sorted array of distinct integers, write a function index_equals_value that returns the lowest index for which array[index] == index.
Return -1 if there is no such index.

Your algorithm should be very performant.

[input] array of integers ( with 0-based nonnegative indexing )
[output] integer

Examples:

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

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

Random Tests Constraints:

Array length: 200 000

Amount of tests: 1 000

Time limit: 1.5 s


If you liked this Kata check out my more complex creations:

Find the neighbourhood in big dimensions. Neighbourhood collection

The Rubik's cube

Arrays
Algorithms

Stats:

CreatedAug 13, 2018
PublishedAug 13, 2018
Warriors Trained5444
Total Skips119
Total Code Submissions59375
Total Times Completed1998
Python Completions504
JavaScript Completions1404
Haskell Completions30
Rust Completions102
Total Stars149
% of votes with a positive feedback rating88% of 303
Total "Very Satisfied" Votes255
Total "Somewhat Satisfied" Votes26
Total "Not Satisfied" Votes22
Total Rank Assessments7
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • sgerodes Avatar
  • imjasonmiller Avatar
  • docgunthrop Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • Diapolo10 Avatar
Ad