<?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: The missing number</title>
	<atom:link href="http://www.fsharp.it/2008/07/01/the-missing-number/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fsharp.it/2008/07/01/the-missing-number/</link>
	<description>Functional programming on .Net</description>
	<lastBuildDate>Wed, 18 Aug 2010 12:04:06 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Vlastimil Adamovsky</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-6220</link>
		<dc:creator>Vlastimil Adamovsky</dc:creator>
		<pubDate>Mon, 20 Jul 2009 20:17:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-6220</guid>
		<description>Better solution in c# using Linq:

int missingNumber(IEnumerable myCol)
        {
            return (myCol.Count() + 1) * myCol.Count() / 2 - myCol.Sum();
        }</description>
		<content:encoded><![CDATA[<p>Better solution in c# using Linq:</p>
<p>int missingNumber(IEnumerable myCol)<br />
        {<br />
            return (myCol.Count() + 1) * myCol.Count() / 2 &#8211; myCol.Sum();<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vlastimil Adamovsky</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-6218</link>
		<dc:creator>Vlastimil Adamovsky</dc:creator>
		<pubDate>Mon, 20 Jul 2009 18:14:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-6218</guid>
		<description>try this one, writtem in C# and using Linq:

int missingNumber(IEnumerable list)
        {  
            var k = new List(list);
            k.Sort();
            return k.Last() != k.Count() + 1 ? k.Count() + 1 : k.Where((elem, index) =&gt; (index + 1) != elem).First() - 1;
        }</description>
		<content:encoded><![CDATA[<p>try this one, writtem in C# and using Linq:</p>
<p>int missingNumber(IEnumerable list)<br />
        {<br />
            var k = new List(list);<br />
            k.Sort();<br />
            return k.Last() != k.Count() + 1 ? k.Count() + 1 : k.Where((elem, index) =&gt; (index + 1) != elem).First() &#8211; 1;<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ZZ</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-733</link>
		<dc:creator>ZZ</dc:creator>
		<pubDate>Wed, 03 Sep 2008 18:12:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-733</guid>
		<description>[c#]
public static int findMissingNumber3(final int[] numbers)
	{	
		int missing = 0;
		for (int i=0; i&lt;numbers.length; i++)
		{
			missing += (i+1 - numbers[i]);
		}
		return missing;
	}
[/c#]</description>
		<content:encoded><![CDATA[<pre class="brush: c#">
public static int findMissingNumber3(final int[] numbers)
	{
		int missing = 0;
		for (int i=0; i&lt;numbers.length; i++)
		{
			missing += (i+1 - numbers[i]);
		}
		return missing;
	}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Soumen Sarkar</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-546</link>
		<dc:creator>Soumen Sarkar</dc:creator>
		<pubDate>Sun, 20 Jul 2008 01:32:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-546</guid>
		<description>Just because there is a less chance doesnt make the method better, there is still a chance of an overflow. Hence I have provided a method which do NOT use any integer summing. Instead I use cumulative XOR to cancel out duplicates. Here is the third implementation:

[code language=c++]
/**
* @param sequence : Sequence of length N Contains integers 1 to (N + 1)
* with one number missing. The array is unsorted.
*
* @return The missing number
*
* Algorithm: In this case we do not calculate any integer sum at all. We do cumulative 
* XOR of all elements with index. Please note that N XOR N == 0. So all elements eventually
* cancel all index and last number standing after cumulative XOR is the missing number. 
* For example consider the sequence [1] [2] [3] [?].
* So 1 XOR 1 == 0, 2 XOR 2 == 0, 3 XOR 3 == 0, so last number standing will be 4. 
* It does not matter if numbers are permuted instead of being sorted.
*/

public static int method_3(int [] intSeq)
{
int missingNum = intSeq.length + 1;
for (int i = 0; i &lt; intSeq.length; ++i)
{
missingNum ^= ((i+1) ^ intSeq[i]);
}

return missingNum;
}
[/code]</description>
		<content:encoded><![CDATA[<p>Just because there is a less chance doesnt make the method better, there is still a chance of an overflow. Hence I have provided a method which do NOT use any integer summing. Instead I use cumulative XOR to cancel out duplicates. Here is the third implementation:</p>
<pre class="brush: c++">
/**
* @param sequence : Sequence of length N Contains integers 1 to (N + 1)
* with one number missing. The array is unsorted.
*
* @return The missing number
*
* Algorithm: In this case we do not calculate any integer sum at all. We do cumulative
* XOR of all elements with index. Please note that N XOR N == 0. So all elements eventually
* cancel all index and last number standing after cumulative XOR is the missing number.
* For example consider the sequence [1] [2] [3] [?].
* So 1 XOR 1 == 0, 2 XOR 2 == 0, 3 XOR 3 == 0, so last number standing will be 4.
* It does not matter if numbers are permuted instead of being sorted.
*/

public static int method_3(int [] intSeq)
{
int missingNum = intSeq.length + 1;
for (int i = 0; i &amp;lt; intSeq.length; ++i)
{
missingNum ^= ((i+1) ^ intSeq[i]);
}

return missingNum;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Ford</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-538</link>
		<dc:creator>Chris Ford</dc:creator>
		<pubDate>Fri, 18 Jul 2008 16:33:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-538</guid>
		<description>Here&#039;s a solution in Python that doesn&#039;t have the overflow/linear space issue. It operates in linear time because the sorting works a bit like a keysort. It operates in constant space because by sorting the original list in-place.

Once the list is sorted it&#039;s easy to find the missing number.

[code language=python]

null = 0

def findMissing( sequence ):

	# Pad the list to fit the missing value
	sequence.append( null ) 

	# Sort the list
	sequenceSort( sequence )

	# Find &#039;null&#039; index and hence missing value 
	return sequence.index( null ) + 1


# Sorts a sequence (in linear time and constant space) 
# Assumes the list is large enough to fit all values
def sequenceSort( sequence ):
	index = 0
	while( index &lt; len(sequence) ):

		# Pull out a value and set its spot to &#039;null&#039;
		value = sequence[index]
		sequence[index] = null 

		# Insert the value into the list.
		# Keep inserting the evicted values till we fill the slot we first got
		# the value from. 
		while( True ):
			value = insert( value, sequence )
			if( (value == sequence[index]) or (value == null) ):
				break	

		index = index + 1

	return sequence 


# Inserts a value into the list, returning the value
# that used to live at that index.
def insert( value, sequence ):
	index = value - 1
	evicted = sequence[index]
	sequence[index] = value
	return evicted

[/code]

I&#039;d be interested to see what this looks like in F#.  Since my solution depends on the mutability of the original list, it&#039;s hard to see how you could do it in a nice, clean, functional way.</description>
		<content:encoded><![CDATA[<p>Here&#8217;s a solution in Python that doesn&#8217;t have the overflow/linear space issue. It operates in linear time because the sorting works a bit like a keysort. It operates in constant space because by sorting the original list in-place.</p>
<p>Once the list is sorted it&#8217;s easy to find the missing number.</p>
<pre class="brush: python">

null = 0

def findMissing( sequence ):

	# Pad the list to fit the missing value
	sequence.append( null ) 

	# Sort the list
	sequenceSort( sequence )

	# Find &#039;null&#039; index and hence missing value
	return sequence.index( null ) + 1

# Sorts a sequence (in linear time and constant space)
# Assumes the list is large enough to fit all values
def sequenceSort( sequence ):
	index = 0
	while( index &amp;lt; len(sequence) ):

		# Pull out a value and set its spot to &#039;null&#039;
		value = sequence[index]
		sequence[index] = null 

		# Insert the value into the list.
		# Keep inserting the evicted values till we fill the slot we first got
		# the value from.
		while( True ):
			value = insert( value, sequence )
			if( (value == sequence[index]) or (value == null) ):
				break	

		index = index + 1

	return sequence 

# Inserts a value into the list, returning the value
# that used to live at that index.
def insert( value, sequence ):
	index = value - 1
	evicted = sequence[index]
	sequence[index] = value
	return evicted
</pre>
<p>I&#8217;d be interested to see what this looks like in F#.  Since my solution depends on the mutability of the original list, it&#8217;s hard to see how you could do it in a nice, clean, functional way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SS</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-535</link>
		<dc:creator>SS</dc:creator>
		<pubDate>Fri, 18 Jul 2008 05:08:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-535</guid>
		<description>I have improved method_2 over method_1. I think in method_2 there is less chance of integer overflow.

[code language=c++]
public static int method_1(int [] sequence)
{
// this can overflow
int allSum = ((sequence.length + 1) * (sequence.length + 2)) / 2;

int seqSum = 0;
for (int i = 0; i &lt; sequence.length; ++i)
{
// this can overflow
seqSum += sequence[i];
}

return allSum - seqSum;
}

/**
* @param sequence : Sequence of length N Contains integers 1 to (N + 1)
* with one number missing. The array is unsorted.
*
* @return The missing number
*/
public static int method_2(int [] sequence)
{
int missingNum = sequence.length + 1;
for (int i = 0; i &lt; sequence.length; ++i)
{
missingNum += ((i+1) - sequence[i]);
}

return missingNum;
}
[/code]</description>
		<content:encoded><![CDATA[<p>I have improved method_2 over method_1. I think in method_2 there is less chance of integer overflow.</p>
<pre class="brush: c++">
public static int method_1(int [] sequence)
{
// this can overflow
int allSum = ((sequence.length + 1) * (sequence.length + 2)) / 2;

int seqSum = 0;
for (int i = 0; i &amp;lt; sequence.length; ++i)
{
// this can overflow
seqSum += sequence[i];
}

return allSum - seqSum;
}

/**
* @param sequence : Sequence of length N Contains integers 1 to (N + 1)
* with one number missing. The array is unsorted.
*
* @return The missing number
*/
public static int method_2(int [] sequence)
{
int missingNum = sequence.length + 1;
for (int i = 0; i &amp;lt; sequence.length; ++i)
{
missingNum += ((i+1) - sequence[i]);
}

return missingNum;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Ford</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-534</link>
		<dc:creator>Chris Ford</dc:creator>
		<pubDate>Mon, 14 Jul 2008 12:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-534</guid>
		<description>I think there is an O(n) solution. We should be able to sort the array in O(n) time because we can tell where a number should appear in the sorted list from its value. As I said in my last post, once we have a sorted list finding the missing number is easy.

I&#039;ll give it a go when I get a little free time.</description>
		<content:encoded><![CDATA[<p>I think there is an O(n) solution. We should be able to sort the array in O(n) time because we can tell where a number should appear in the sorted list from its value. As I said in my last post, once we have a sorted list finding the missing number is easy.</p>
<p>I&#8217;ll give it a go when I get a little free time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: claudio</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-522</link>
		<dc:creator>claudio</dc:creator>
		<pubDate>Thu, 10 Jul 2008 06:56:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-522</guid>
		<description>Eventually, I got yoour point! :)

By the way, is there a solution that uses O(1) memory and has O(n) computational complexity?</description>
		<content:encoded><![CDATA[<p>Eventually, I got yoour point! <img src='http://www.fsharp.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>By the way, is there a solution that uses O(1) memory and has O(n) computational complexity?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Ford</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-521</link>
		<dc:creator>Chris Ford</dc:creator>
		<pubDate>Wed, 09 Jul 2008 23:39:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-521</guid>
		<description>I&#039;m fairly confident :)

If you read through the comments on the post for this question you&#039;ll see that Jesus DeLaTorre and Heiko Hatzfeld have made similar observations. I accept it&#039;s a very pedantic point though.

One way to do it with truly O(1) memory usage would be to sort the original array using an in-place sort like quicksort. Once the list was in order it would then be trivial to step through it and find the missing number.

Of course, the computational complexity of my solution is O(n^2), which is far less efficient than yours.</description>
		<content:encoded><![CDATA[<p>I&#8217;m fairly confident <img src='http://www.fsharp.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you read through the comments on the post for this question you&#8217;ll see that Jesus DeLaTorre and Heiko Hatzfeld have made similar observations. I accept it&#8217;s a very pedantic point though.</p>
<p>One way to do it with truly O(1) memory usage would be to sort the original array using an in-place sort like quicksort. Once the list was in order it would then be trivial to step through it and find the missing number.</p>
<p>Of course, the computational complexity of my solution is O(n^2), which is far less efficient than yours.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: claudio</title>
		<link>http://www.fsharp.it/2008/07/01/the-missing-number/comment-page-1/#comment-519</link>
		<dc:creator>claudio</dc:creator>
		<pubDate>Wed, 09 Jul 2008 07:58:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.fsharp.it/?p=36#comment-519</guid>
		<description>Hi Chris,
are you sure about your assertion?

According to Michael&#039;s comment, I understood that I did correctly, and in addition my solution was accepted by the authors.</description>
		<content:encoded><![CDATA[<p>Hi Chris,<br />
are you sure about your assertion?</p>
<p>According to Michael&#8217;s comment, I understood that I did correctly, and in addition my solution was accepted by the authors.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
