Author Topic: Sequence Problem  (Read 4569 times)

0 Members and 1 Guest are viewing this topic.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Sequence Problem
« on: June 21, 2007, 08:50:44 am »
My turn!

My friend posed a problem at me yesterday (and gave me the solution, thankfully :) ). I figured I'd post it here and see if others come to the same solution.

You have a list of numbers from 1 to n. Exactly one of the numbers is repeated once, and the rest of the numbers are present. The list is not in order. What is the best way to determine which number is repeated?

(Note: sorting the list then doing a search is O(nlog(n)), which is not the best solution)

(Hint: you only have to go over the list once, so the best solution is O(n)).

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Sequence Problem
« Reply #1 on: June 23, 2007, 12:10:57 am »
Reorder the list as you go along, putting high values at the end and low values at the beginning.  When it's done, the number that occurs twice will be next to itself.

IE:
1. akfhgidecgjb
2. afhgidecgjbk
3. ahgidecgjbfk
4. agidecgjbfhk
5. aidecgjbfghk
6. adecgjbfghik
...
10
10. acdegjbfghik
11. acdegbfghijk
12. abcdegfghijk
13. abcdefgghijk

It required n+1 steps, but that's pretty close (even though I think it's the wrong answer).  I'm not sure if it would be n+1 for ANY n, but eh?

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Sequence Problem
« Reply #2 on: June 23, 2007, 12:16:27 am »
What you're doing is essentially sorting and then searching.  I don't think its fair to say "put things in order as you go along," since all you're doing when you're "going along" is sorting. :)

Also, n+1 steps is still O(n).

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Sequence Problem
« Reply #3 on: June 23, 2007, 12:40:54 am »
hah :P

You're right.  It's been a while since I've used/learned orders.

Anyway, I think the algorithm for moving each letter isn't O(n), so it doesn't really matter........

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Sequence Problem
« Reply #4 on: June 23, 2007, 12:46:05 am »
Yeah, that's correct.  Moving a letter is constant time, but finding where to put it isn't.

Offline Rule

  • x86
  • Hero Member
  • *****
  • Posts: 1588
    • View Profile
Re: Sequence Problem
« Reply #5 on: June 23, 2007, 12:45:42 pm »
(Edit): I had a solution for log2n time, but I realised I hadn't read the problem carefully enough.



« Last Edit: June 23, 2007, 12:48:54 pm by Rule »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Sequence Problem
« Reply #6 on: July 06, 2007, 01:11:16 pm »
I guess I'll give a hint: the formula for the sum of 1..n is n(n+1)/2.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Sequence Problem
« Reply #7 on: July 06, 2007, 01:44:46 pm »
:P
Sum all the numbers and subtract the sum of the numbers 1..n with no repeats.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Sequence Problem
« Reply #8 on: July 06, 2007, 01:47:24 pm »
Exactly.

Now, what happens if the size of n approaches INT_MAX? How do you solve this without summing the numbers?

(hint: uses XOR)