programming-examples/ruby/Arrays/FixedPointInUnsorted.rb

17 lines
374 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
=begin
Given an array, find a fixed point in the array.
Fixed Point in an array is an index i such that arr[i] is equal to i, i.e. a[i]==i
Time-complexity: O(n)
Space-complexity: O(1)
Algorithm: Linear Search
=end
def fixed_point(a)
n=a.length
for i in 0...n
if a[i]==i
return i
end
end
#if no fixed point exists
return -1
end