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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:53:56+00:00 2026-05-18T04:53:56+00:00

We have been given an array of size N that contains integers in the

  • 0

We have been given an array of size N that contains integers in the range 0 to N-2, both inclusive.

The array can have multiple repeated entries. We need to find one of the duplicated entries in O(N) time and constant space.

I was thinking of taking the product and sum of all the entires in the array, and the product and sum of all the numbers in the range 0 to N-2.

Then, the difference of the sums and the division of the products would give us two equations. This approach would work if it were given that there are only two repeated entries, but since there can be more than two, I think my approach fails.

Any suggestions?

Edit: The array is immutable. I realize that this is an important piece of information and I apologize that I forgot to include this earlier.

  • 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-18T04:53:57+00:00Added an answer on May 18, 2026 at 4:53 am

    Here’s a nice treatment. It passes through some easier problems before addressing this one.

    http://aperiodic.net/phil/archives/Geekery/find-duplicate-elements.html

    It contains a solution for when you can modify the input array, and another for when you can’t.

    Brief summary in case the link ever dies: the array indexes run from 0 .. N-1, and the array values run 0 .. N-2. Each array element can therefore be considered as an index (or “pointer”) into the array itself: element i “points to” element ra[i], ra[i] points to ra[ra[i]] and so on. By repeatedly following these pointers, me must eventually enter a cycle, because we certainly can’t go forever without revisiting some node or other.

    Now, the very last element, N-1, isn’t pointed to by any other element. So if we start there and eventually enter a cycle, somewhere along the way there must be an element which can be reached from two different places: the route we took the first time, and the route which is part of the cycle. Something like this:

      N-1 -> a1 -> a2 -> a3
                   ^       \
                  /         v
                a6 <- a5 <- a4
    

    In this case, a2 is reachable from two different places.

    But a node which is reachable from two different places is precisely what we’re looking for, a duplicate in the array (two different array elements containing the same value).

    The question then is how to identify a2, and the answer is to use Floyd’s cycle-finding algorithm. In particular it tells us the “start” of the loop in O(N) time and O(1) space.

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

Sidebar

Related Questions

I have a Width and Height parametr. I have been given an array of
I have a 2D array of integers that represent groupings (crystal grains) on a
Good day, I have been given an assignment that gets data off a CSV
For School I have been given a class interface to define my own array
I have been given control of a web site in Classic ASP. What is
I have been given some poorly formatted data and need to pull numbers out
I have been given an exercise to solve the zebra puzzle using a constraint
I have been given the below .NET question in an interview. I don’t know
We have been given a site xyz.com, in which the home page has images
I have been given the following request. Please give 7% of the current contacts

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.