• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

August 12, 2014 by Dhaval Dave

Given an array of n distinct integers sorted in ascending order, write a function that returns a Fixed Point in the array such that Arr[i] = i .
If there is any Fixed Point present in array, else returns -1.
Note that integers in array can be negative.

Example :

  Input: arr[] = {-2, -1, 1, 3, 5}
Output: 3 // arr[3] == 3

Input: arr[] = {-4, -1, 1, 2, 5, 8}
Output: -1 // No Fixed Point

First Simple Method : Linear Search
for(i = 0; i < n; i++){
        if(arr[i] == i) return i;
}
Complexity O(n)

Method 2 : Divide and Conquer (As numbers is sorted we can use it as binary search)

Thanks to Dhaval for suggesting this Code.

int indexSearch(int arr[], int low, int high)
{
    if(high >= low)
    {
        int mid = (low + high)/2;
        if(mid == arr[mid])
            return mid;
        if(mid > arr[mid])
            return indexSearch(arr, (mid + 1), high);
        else
            return indexSearch(arr, low, (mid -1));
    }

    /* Return -1 if there is no Fixed Point */
    return -1;
}
int main()
{
    int arr[] = {-2, -1, 1, 3, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    printf(“Fixed Point is %d”, indexSearch(arr, 0, n-1));
    getchar();
    return 0;
}

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Hacker Earth Questions, Interview Questions, problem Tagged With: Array

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Walmart Labs Interview Experience

Find the kth number with prime factors 3, 5 and 7

Python Dictionaries

Find Pythagorean Triplets in an array in O(N)

VMWare Openings

Printing Longest Common Subsequence

Amazon Interview Experience – SDE Chennai

Convert number to words java

Difference between a LinkedList and a Binary Search Tree BST

Check if an array has duplicate numbers in O(n) time and O(1) space

Number of Islands BFS/DFS

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Find Nearest Minimum number in left side in O(n)

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

SAP Hiring Off-Campus General Aptitude

Linked List V/S Binary Search Tree

Practo Hiring Experience

Print Power Set of a Set

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Sort Stack in place

Python List

Word Break Problem

Given a string, find the first character which is non-repetitive

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

SAP Off Campus Hiring_ March 2015 Verbal Skills

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Memory Efficient LinkedList

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Code Chef PRGIFT Solution

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Copyright © 2025 · Genesis Framework · WordPress · Log in