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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:46:11+00:00 2026-05-23T07:46:11+00:00

Given an array of n+1 integers, each in the range 1 to n ,

  • 0

Given an array of n+1 integers, each in the range 1 to n, find an integer that is repeated.

I was asked this at a job interview. Here’s my answer: The Pigeonhole Principle says there has to be a repeat. I tried to use a binary search approach, so I did this in Matlab, because that’s what I know:

top = 0;
bot = 0;
for i=1:n+1
  if P[i] > n/2 
    top = top+1;
  else
    bot = bot+1;
end

So then I argue that one of these, top or bot, has to be bigger than n/2 by the PhP again. Take that range and repeat.

I thought this was a pretty good solution, but the interviewer sort of hinted that one can do better. Please post any better solutions you know of.

  • 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-23T07:46:11+00:00Added an answer on May 23, 2026 at 7:46 am

    I’m not sure how you’re defining “better”, but perhaps this qualifies. At least it’s different from your solution and the solutions to the linked list questions (pun intended).

    If we make a path

    n+1 --> array[n+1] --> array[array[n+1]] --> ...
    

    then this path contains a cycle if and only if array^k[n+1] = array^l[n+1] for some k != l, that is, if and only if there is a repeat. The question now becomes a common linked list problem that can be solved as follows.

    Start two particles on the first node. Let the first particle move at unit speed and let the second particle move at twice unit speed. Then if there is a cycle, the second particle will end up looping back behind the first, and eventually they’ll be the same. Why? Well, if you think of the particles as on a circle (which they will be once the find the loop), at every time unit the second particle gets one directed step closer to the first. Therefore they must eventually collide. One they do, you’ve found a loop. To find the repeated value, simply get the length of the loop by letting one particle stand still while the other runs the loop again. Then start both particles at the start again, let one move the length of the loop ahead, and then run both particles together with constant distance between them until they meet again at the beginning of the loop.

    As some commentators are outraged that I did not include all of the details of how to find a loop in a linked list, here it now is. No promises that this isn’t buggy (it’s Matlab-esque pseudocode after all), but it should at least explain the idea.

    %% STEP 1: find a point in the cycle of the linked list using a slow and fast particle
    slow = n+1;
    fast = n+1;
    for i=1 to n+1
        slow = array[slow];
        fast = array[array[fast]];
        if (slow == fast)
            break;
    end
    
    %% STEP 2: find the length of the cycle by holding one particle fixed
    length = 1;
    fast = array[fast]
    while fast != slow
        fast = array[fast];
        length = length+1;
    end
    
    %% STEP 3: find the repeated element by maintaining constant distance between particles
    slow = n+1;
    fast = n+1;
    for i=1 to length
        fast = array[fast];
    end
    while fast != slow
        fast = array[fast];
        slow = array[slow];
    end
    
    %% STEP 4: return the repeated entry
    return slow;
    

    Here I started at n+1 because array[i] is between 1 and n, so neither particle will ever be sent back to n+1. This makes at most one pass through the array (though not in order) and keeps track of two particles (slow and fast) and one integer (length). The space is therefore O(1) and the time is O(n).

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

Sidebar

Related Questions

I have an assignment that is the following: For a given integer array, find
Given an array of integers ,You have to find two elements whose XOR is
Given is an array of integers. Each number in the array occurs an ODD
Consider this problem: You are given an array containing positive integers. All the integers
Given an array of positive integer, we need to exchange each element with first
Given an array of integers, what is the simplest way to iterate over it
Given an unsorted integer array, and without making any assumptions on the numbers in
You are given a 32-bit unsigned integer array with length up to 2 32
Given an array of items, each of which has a value and cost ,
I have a 2D array of integers that represent groupings (crystal grains) on a

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.