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 6068161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:41:09+00:00 2026-05-23T09:41:09+00:00

Please read this question carefully before closing it as a duplicate, though if it

  • 0

Please read this question carefully before closing it as a duplicate, though if it is an honest duplicate I’ll be happy to know about it. This is a generalization of Find any one of multiple possible repeated integers in a list.

Given any set S of N distinct
integers, and any array A of
length N+1 each entry of which is
taken from S, what is the best
algorithm to find some (there must be
at least one) repeated entry of A?

NOTE: There could be multiple repeated entries in A, and any entry could be repeated multiple times.

As Nemo points out, the trivial solution takes O(1) space and O(N^2) time. I’m looking for a solution that improves the time without compromising the space too much. To be precise, the solution(s) I’m looking for:

  • Returns a value a that appears in A at least twice,
  • Uses at most O(log N) space without modifying A, and
  • Takes less than O(N^2) time

EDIT: The set S is there to ensure that the array A has at least one repeated entry. For this problem, do not assume that you have S given to you as an ordered set. You can query S (boolean to return true is s in S and false otherwise) and you can query A (call for A[i]), but that’s all. Any solution that sorts A or S exceeds the space limit.

This generalization invalidates my pointer solution to the original question (which has O(1) space and O(N) time), and the space constraint I’m imposing invalidates fiver’s solution (which has O(N) space and time).

  • 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-23T09:41:09+00:00Added an answer on May 23, 2026 at 9:41 am

    This algorithm is similar to Justin Simon’s, but the key point is how to compute the median (or the kth element) of S using just O(1) space efficiently.

    Here is that key algorithm, which is randomized:

    Set lower equal to the minimum element of S and upper equal to the maximum element of S. Pick a random element x from S that is between lower and upper (this costs at most O(n) expected time). Compute the rank of x (O(n) time). If x’s rank is too low, set lower to the successor of x (O(n) time), else set upper equal to the predecessor of x (O(n) time). Repeat until lower equals upper.

    Note that each iteration costs O(n) in expectation and there are O(lg n) iterations in expectation so the expected time cost is O(n lg n) and space usage is O(1) since we only store lower and upper.

    Using this ability to select the kth element, we can then use the pigeonhole principle as suggested in the original question to find increasingly small segments of S that contain too many elements to all be distinct, using O(lg n) linear scans of A and O(1) space to store the relevant sums of elements in each region. Each such iteration costs O(n) in addition to the O(n lg n) cost of finding the kth element, and there are O(lg n) iterations, so the total cost is O(n lg^2 n).

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

Sidebar

Related Questions

Before this question is marked as duplicate, please read it. ;) There are already
Guys before you start down voting me please read this question and please understand
Please read this entire question before you try to mark this as too controversial.
This is quite a complicated and brain busting question, so please read carefully and
Ok before you think Not another question like this please read this first. I
Update: Please read this question in the context of design principles, elegance, expression of
I know this might be a no-brainer, but please read on. I also know
I am curious to know why this is happening. Please read the code example
Hi all (before closing based on the title, please read the bottom parragraph), I'm
Please read the whole question; I personally think that this is programming-related; if you

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.