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

  • Home
  • SEARCH
  • 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 126501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:19:40+00:00 2026-05-11T05:19:40+00:00

I have a sorted array of 5000 integers. How fast can I tell if

  • 0

I have a sorted array of 5000 integers. How fast can I tell if a random integer is a member of the array? An answer in general, C and Ruby would be nice.

The array values are of the form

c * c + 1 

where c can be any integer from 1 to 5000.

For example:

[2, 5, 10, 17, 26, 37, 50 ...] 
  • 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. 2026-05-11T05:19:40+00:00Added an answer on May 11, 2026 at 5:19 am

    Binary search, as others have mentioned, is O(log2N), and can be coded either recursively:

       BinarySearch(A[0..N-1], value, low, high) {        if (high < low)            return -1 // not found        mid = (low + high) / 2        if (A[mid] > value)            return BinarySearch(A, value, low, mid-1)        else if (A[mid] < value)            return BinarySearch(A, value, mid+1, high)        else            return mid // found    } 

    or iteratively:

       BinarySearch(A[0..N-1], value) {        low = 0        high = N - 1        while (low <= high) {            mid = (low + high) / 2            if (A[mid] > value)                high = mid - 1            else if (A[mid] < value)                low = mid + 1            else                return mid // found        }        return -1 // not found    } 

    However, if you’re looking for the fastest possible way, you can set up a look up table based on the sqrt(N-1) of your numbers. With just 5,000 words of memory you can achieve O(1) lookups this way.

    Explanation:

    Since all your numbers are of the form N^2 + 1 for an integer N from 1 to N, you can create a table of N elements. The element at position i will specify if i^2 + 1 is in your array or not. The table can be implemented with a simple array of length N. It will take O(N) to build, and N words of space. But once you have the table, all lookups are O(1).

    Example:

    Here’s sample code in Python, which reads like pseudocode, as always 🙂

    import math  N = 5000 ar = [17, 26, 37, 50, 10001, 40001]  lookup_table = [0] * N  for val in ar:     idx = int(math.sqrt(val - 1))     lookup_table[idx] = 1  def val_exists(val):     return lookup_table[int(math.sqrt(val - 1))] == 1  print val_exists(37) print val_exists(65) print val_exists(40001) print val_exists(90001) 

    Building the table takes up O(N) at most, and lookups are O(1).

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

Sidebar

Related Questions

Suppose I have a sorted array of integers int[] , and I want to
I have a sorted integer array on the device, e.g.: [0,0,0,1,1,2,2] And I want
I have an array(sorted on an attribute called node_id) of ruby objects which basically
I have an array of string that I would like to have sorted based
Given an integer k and an sorted array A (can consist of both positive
I have a sorted array of doubles (latitudes actually) that relatively uniformally spread out
I have a sorted mutable array of a class called Topic. The topics represent
1-)For sorted array I have used Binary Search. We know that the worst case
I have a array with users sorted by score. I only want to output
I have an array of values which is almost, but not quite sorted, with

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.