public static int GetMissingNumberFromArray(int[] arr)
Console.WriteLine("=======================");
for(int i = 1; i <= arr.Length + 1; i++){
Console.WriteLine("totalXor : {0}", totalXor);
foreach(int i in arr) totalArrXor ^= i;
Console.WriteLine("totalArrXor : {0}", totalArrXor);
return totalXor ^ totalArrXor;
public static int[] GetMissingTwoNumbers(int[] arr){
int size = arr.Length + 2;
Console.WriteLine("size : {0}", size);
long totalSum = size*(size+1)/2;
Console.WriteLine("totalSum : {0}", totalSum);
foreach(int i in arr) arrSum += i;
Console.WriteLine("arrSum : {0}", arrSum);
int pivot = (int)((totalSum - arrSum) / 2);
Console.WriteLine("pivot : {0}", pivot);
int totalXorArrayRight = 0;
int totalXorArrayLeft = 0;
for(int i = 1; i <= pivot; i++) totalXorLeft ^= i;
Console.WriteLine("totalXorLeft : {0}", totalXorLeft);
for(int i = pivot +1; i <= size; i++) totalXorRight ^= i;
Console.WriteLine("totalXorRight : {0}", totalXorRight);
Console.WriteLine("totalXorArrayLeft : {0}", totalXorArrayLeft);
Console.WriteLine("totalXorRight : {0}", totalXorArrayRight);
return new int[] {(totalXorLeft ^ totalXorArrayLeft), (totalXorRight ^ totalXorArrayRight)};
public static void Main()
int[] testArrayMissing2 = {1,2,5,6};
int[] testArrayMissing1 = {1,2,4,5,6};
int[] missingTwoNumbers = GetMissingTwoNumbers(testArrayMissing2);
int missingOneNumber = GetMissingNumberFromArray(testArrayMissing1);
Console.WriteLine("=======================");
foreach(int i in missingTwoNumbers) Console.WriteLine("Number Missing : {0}", i);
Console.WriteLine("--------------------");
Console.WriteLine("Number Missing single : {0}", missingOneNumber);