<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Find the Duplicate</title>
	<atom:link href="http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/feed/" rel="self" type="application/rss+xml" />
	<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/</link>
	<description>Strain your Brain</description>
	<lastBuildDate>Tue, 13 Sep 2011 13:21:47 -0700</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: yaniv</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-40999</link>
		<dc:creator>yaniv</dc:creator>
		<pubDate>Tue, 31 Aug 2010 17:39:33 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-40999</guid>
		<description>A clarification - the solution takes O(log(N)) bits of memory and O(N * logN) time, if you are only allowed actions on bits. Note that in this case the size of the input is N*log(N).
It is simpler to count the complexity in integers, in which case it is indeed O(N) running time and O(1) memory.</description>
		<content:encoded><![CDATA[<p>A clarification &#8211; the solution takes O(log(N)) bits of memory and O(N * logN) time, if you are only allowed actions on bits. Note that in this case the size of the input is N*log(N).<br />
It is simpler to count the complexity in integers, in which case it is indeed O(N) running time and O(1) memory.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: david</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-252</link>
		<dc:creator>david</dc:creator>
		<pubDate>Wed, 31 Oct 2007 19:35:46 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-252</guid>
		<description>good one!

p.s
I couldn&#039;t find the porn, is it in your previous posts?</description>
		<content:encoded><![CDATA[<p>good one!</p>
<p>p.s<br />
I couldn&#8217;t find the porn, is it in your previous posts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yaniv</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-202</link>
		<dc:creator>yaniv</dc:creator>
		<pubDate>Sun, 14 Oct 2007 21:03:18 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-202</guid>
		<description>Dan,

I am very sorry that comment got cut-off by the stupid html filtering mechanism I am using. It removes everything between the &lt; and the &gt; signs.

Please use the symbols: ampersand, &#039;l&#039;, &#039;t&#039;, and semicolon (4 symbol all together) for the &lt; sign. The &gt; sign can be used regularly.

The solution is really cool.

In a slightly different situation, if you knew that a cycle existed and had good reason to suspect that its length (and the length of the tail leading to it) is &lt;&lt; N (wow! almost got my comment cut-off there... ;-) ) then instead of performing the initial step N times (which is of course the logical thing to do in this riddle where you cannot make the previous assumption) you could start with x = 0 and y = 0 and each turn advance x by one step and advance y by 2 steps. When x == y, you will know that you are inside the cycle. Pollard&#039;s Rho algorithm (used mainly for factoring) uses this technique.</description>
		<content:encoded><![CDATA[<p>Dan,</p>
<p>I am very sorry that comment got cut-off by the stupid html filtering mechanism I am using. It removes everything between the &lt; and the &gt; signs.</p>
<p>Please use the symbols: ampersand, &#8216;l&#8217;, &#8216;t&#8217;, and semicolon (4 symbol all together) for the &lt; sign. The &gt; sign can be used regularly.</p>
<p>The solution is really cool.</p>
<p>In a slightly different situation, if you knew that a cycle existed and had good reason to suspect that its length (and the length of the tail leading to it) is &lt;&lt; N (wow! almost got my comment cut-off there&#8230; <img src='http://leviathanonline.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ) then instead of performing the initial step N times (which is of course the logical thing to do in this riddle where you cannot make the previous assumption) you could start with x = 0 and y = 0 and each turn advance x by one step and advance y by 2 steps. When x == y, you will know that you are inside the cycle. Pollard&#8217;s Rho algorithm (used mainly for factoring) uses this technique.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-201</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Sun, 14 Oct 2007 20:51:36 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-201</guid>
		<description>It appears that I was not careful enough not to use the lesser-than sign, and some parts got cut off.

int i, d, x, y;
for(i=0, x=0; i!=N; i++, x=v[x]);
for(d=1, y=v[x]; y!=x; d++, y=v[y]);
for(i=0, y=0; i!=d; i++, y=v[y]);
for(x=0; x!=y; x=v[x], y=v[y]);
return x;

Start of Explanation:

We think of the vector as directed graph, with nodes 0, ... , N-1, and each node i has exactly one edge leaving it, and entering node v[i]. In particular, no edge enters node 0, and a node is a duplicate element iff its entering degree is greater than 1. [The rest of the explanation continues above]</description>
		<content:encoded><![CDATA[<p>It appears that I was not careful enough not to use the lesser-than sign, and some parts got cut off.</p>
<p>int i, d, x, y;<br />
for(i=0, x=0; i!=N; i++, x=v[x]);<br />
for(d=1, y=v[x]; y!=x; d++, y=v[y]);<br />
for(i=0, y=0; i!=d; i++, y=v[y]);<br />
for(x=0; x!=y; x=v[x], y=v[y]);<br />
return x;</p>
<p>Start of Explanation:</p>
<p>We think of the vector as directed graph, with nodes 0, &#8230; , N-1, and each node i has exactly one edge leaving it, and entering node v[i]. In particular, no edge enters node 0, and a node is a duplicate element iff its entering degree is greater than 1. [The rest of the explanation continues above]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-200</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Sun, 14 Oct 2007 20:44:31 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-200</guid>
		<description>Just to clear away the confusion, my solution uses O(1) integer variables, each of which indeed requires log(N) memory because we want it to have values up to N. I didn&#039;t take that fine point into account in my previous comment. 

Let us assume the indices of the vector start with 0, and let us denote the i-th element of the vector v[i]. An algorithm, in C code:

int i, d, x, y;
for(i=0, x=0; i1. We start traversing the graph from node 0 and always leaving by the available edge. Eventually (and quite importantly, after traversing N edges at the most), we will revisit a previous node. Let us look at the first node to be revisited: It is not 0, as we cannot enter 0. Thus, we have entered it twice, and from two different nodes, because it is the first to be revisited - so it is a duplicate. Furthermore, if we continue traversing the graph, we will necessarily walk in a cycle [Note: this is related to the hint]. The first loop traverses N edges of the graph, therefore ending inside the aforementioned cycle. Using that knowledge, the second loop calculates the length/period of the cycle, and stores that in d. The third loop then sets y to the d-th element to be visited in the normal walk, and x is set to 0, the beginning of the walk. The fourth loop then traverses the graph with both x and y simultaneously, knowing that they will be equal iff x is in the cycle, because they are always a distance of d off. Therefore when they first become equal, we have found the first element of the cycle, which is our duplicate.</description>
		<content:encoded><![CDATA[<p>Just to clear away the confusion, my solution uses O(1) integer variables, each of which indeed requires log(N) memory because we want it to have values up to N. I didn&#8217;t take that fine point into account in my previous comment. </p>
<p>Let us assume the indices of the vector start with 0, and let us denote the i-th element of the vector v[i]. An algorithm, in C code:</p>
<p>int i, d, x, y;<br />
for(i=0, x=0; i1. We start traversing the graph from node 0 and always leaving by the available edge. Eventually (and quite importantly, after traversing N edges at the most), we will revisit a previous node. Let us look at the first node to be revisited: It is not 0, as we cannot enter 0. Thus, we have entered it twice, and from two different nodes, because it is the first to be revisited &#8211; so it is a duplicate. Furthermore, if we continue traversing the graph, we will necessarily walk in a cycle [Note: this is related to the hint]. The first loop traverses N edges of the graph, therefore ending inside the aforementioned cycle. Using that knowledge, the second loop calculates the length/period of the cycle, and stores that in d. The third loop then sets y to the d-th element to be visited in the normal walk, and x is set to 0, the beginning of the walk. The fourth loop then traverses the graph with both x and y simultaneously, knowing that they will be equal iff x is in the cycle, because they are always a distance of d off. Therefore when they first become equal, we have found the first element of the cycle, which is our duplicate.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yaniv</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-196</link>
		<dc:creator>yaniv</dc:creator>
		<pubDate>Sun, 14 Oct 2007 13:20:16 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-196</guid>
		<description>Dan,

PS - you are welcome to post your solution on the second page.</description>
		<content:encoded><![CDATA[<p>Dan,</p>
<p>PS &#8211; you are welcome to post your solution on the second page.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yaniv</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-195</link>
		<dc:creator>yaniv</dc:creator>
		<pubDate>Sun, 14 Oct 2007 13:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-195</guid>
		<description>No, I did mean O(log(N)). Just to read the values from the buffer you need O(log(N)) memory (the size of the pointer!).</description>
		<content:encoded><![CDATA[<p>No, I did mean O(log(N)). Just to read the values from the buffer you need O(log(N)) memory (the size of the pointer!).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/comment-page-1/#comment-194</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Sun, 14 Oct 2007 12:12:55 +0000</pubDate>
		<guid isPermaLink="false">http://yaniv.leviathanonline.com/blog/riddles/find-the-duplicate/#comment-194</guid>
		<description>I know of a solution, but I&#039;ll withhold it for the time being. Just want to make a clarification: I think you mean O(1) memory complexity. At least, that&#039;s the way I know the riddle.</description>
		<content:encoded><![CDATA[<p>I know of a solution, but I&#8217;ll withhold it for the time being. Just want to make a clarification: I think you mean O(1) memory complexity. At least, that&#8217;s the way I know the riddle.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

