Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3352488
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:02:11+00:00 2026-05-18T02:02:11+00:00

I was asked the following question in my interview yesterday: Consider a Java or

  • 0

I was asked the following question in my interview yesterday:

Consider a Java or C++ array say X which is sorted and no two elements in it are same. How best can you find an index say i such that element at that index is also i. That is X[i] = i.

As clarification she also gave me an example:

Array X : -3 -1 0 3 5 7
index   :  0  1 2 3 4 5

Answer is 3 as X[3] = 3.

The best I could think was a linear search. After the interview I though a lot on this problem but could not find any better solution. My argument is: the element with the required property can be anywhere in the array. So it could also be at the very end of the array so we need to check every element.

I just wanted to confirm from the community here that I’m right. Please tell me I’m right 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:02:11+00:00Added an answer on May 18, 2026 at 2:02 am

    This can be done in O(logN) time and O(1) space by using a slightly modified binary search.

    Consider a new array Y such that Y[i] = X[i] - i

    Array X : -3 -1   0  3  5  7
    index   :  0  1   2  3  4  5
    Array Y : -3 -2  -2  0  1  2
    

    Since the elements in X are in increasing order, the elements in the
    new array Y will be in non-decreasing order. So a binary
    search
    for 0 in Y will give the answer.

    But creating Y will take O(N) space and O(N) time. So instead of
    creating the new array you just modify the binary search such that a
    reference to Y[i] is replaced by X[i] - i.

    Algorithm:

    function (array X) 
           low  = 0
           high = (num of elements in X) - 1
    
           while(low <= high) 
                   mid = (low + high) / 2
    
                   // change X[mid] to X[mid] - mid
                   if(X[mid] - mid == 0)
                           return mid
    
                   // change here too
                   else if(X[mid] - mid < 0)
                           low = mid + 1;
    
                   else
                           high = mid - 1;
           end while
    
           return -1 // no such index exists...return an invalid index.
    
    end function
    

    Java implementation

    C++ implementation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a 3rd-party rotator object, which is providing a smooth, random rotation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.