Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Find the fixed point of an array ...i.e return i if a[i]=i..

1. ## Find the fixed point of an array ...i.e return i if a[i]=i..

Write a C program for the following algorithm

OBJECTIVE: Find the "fixed point" of an array

Input: an array A of *distinct* integers in ascending order.

(Remember that integers can be negative!) The number of

integers in A is n.
Output: one position k in the list, such that A[i]=i, if any exists.

Otherwise: "No".

method 1 :
If A [ i ] > i , we can ignore the right half of the array because

in right half ,for all j > i , we must have A [ j ] > j since A [ i ] >

i, as all the numbers are distinct.
Similarly , if A [ i ] < i, we can ignore the left half of the array

because in left half, for all j < i , we must have A [ j ] < j since A

[ i ] < i ,as all the numbers are distinct.

method 2:
n = size of array
take it=n/2

iterate till it>0&&it<n
if(A[it]>it)
it=it/2
else if(A[it]<it)
it+=it/2
else
return it

if loop returns nothing return error msg

(Post new methods if any ...)

2. Oh that's cool.

Did you need help with something?