Created
July 2, 2013 19:47
-
-
Save riyadparvez/5912494 to your computer and use it in GitHub Desktop.
Find one missing number from an unsorted array of consecutive numbers in C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static int FindMissingNumber(IEnumerable<int> numbers) | |
| { | |
| int expectedSum = (numbers.Count() + 1)*(numbers.Count()+2)/2; | |
| int realSum = 0; | |
| foreach (var number in numbers) | |
| { | |
| realSum += number; | |
| } | |
| return (expectedSum-realSum); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment