Guest User

Untitled

a guest
Mar 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5.  
  6. public static int GetMissingNumberFromArray(int[] arr)
  7. {
  8. Console.WriteLine("=======================");
  9. int totalXor = 0;
  10. int totalArrXor = 0;
  11.  
  12. for(int i = 1; i <= arr.Length + 1; i++){
  13. totalXor ^= i;
  14. }
  15. Console.WriteLine("totalXor : {0}", totalXor);
  16.  
  17.  
  18. foreach(int i in arr) totalArrXor ^= i;
  19. Console.WriteLine("totalArrXor : {0}", totalArrXor);
  20.  
  21. return totalXor ^ totalArrXor;
  22.  
  23. }
  24.  
  25. public static int[] GetMissingTwoNumbers(int[] arr){
  26.  
  27. int size = arr.Length + 2;
  28. Console.WriteLine("size : {0}", size);
  29.  
  30. long totalSum = size*(size+1)/2;
  31. Console.WriteLine("totalSum : {0}", totalSum);
  32.  
  33. long arrSum = 0;
  34. foreach(int i in arr) arrSum += i;
  35. Console.WriteLine("arrSum : {0}", arrSum);
  36.  
  37. int pivot = (int)((totalSum - arrSum) / 2);
  38. Console.WriteLine("pivot : {0}", pivot);
  39.  
  40.  
  41. int totalXorRight = 0;
  42. int totalXorArrayRight = 0;
  43. int totalXorLeft = 0;
  44. int totalXorArrayLeft = 0;
  45.  
  46. //find first array
  47. for(int i = 1; i <= pivot; i++) totalXorLeft ^= i;
  48. Console.WriteLine("totalXorLeft : {0}", totalXorLeft);
  49.  
  50. //find second
  51. for(int i = pivot +1; i <= size; i++) totalXorRight ^= i;
  52. Console.WriteLine("totalXorRight : {0}", totalXorRight);
  53.  
  54. foreach(int i in arr) {
  55. if(i <= pivot){
  56. totalXorArrayLeft ^= i;
  57. } else {
  58. totalXorArrayRight ^= i;
  59. }
  60. }
  61.  
  62. Console.WriteLine("totalXorArrayLeft : {0}", totalXorArrayLeft);
  63.  
  64. Console.WriteLine("totalXorRight : {0}", totalXorArrayRight);
  65.  
  66.  
  67. return new int[] {(totalXorLeft ^ totalXorArrayLeft), (totalXorRight ^ totalXorArrayRight)};
  68. }
  69.  
  70.  
  71.  
  72.  
  73. public static void Main()
  74. {
  75. int[] testArrayMissing2 = {1,2,5,6};
  76.  
  77. int[] testArrayMissing1 = {1,2,4,5,6};
  78.  
  79. int[] missingTwoNumbers = GetMissingTwoNumbers(testArrayMissing2);
  80.  
  81. int missingOneNumber = GetMissingNumberFromArray(testArrayMissing1);
  82.  
  83.  
  84. Console.WriteLine("=======================");
  85.  
  86.  
  87. foreach(int i in missingTwoNumbers) Console.WriteLine("Number Missing : {0}", i);
  88.  
  89.  
  90. Console.WriteLine("--------------------");
  91.  
  92.  
  93. Console.WriteLine("Number Missing single : {0}", missingOneNumber);
  94.  
  95.  
  96. }
  97. }
Add Comment
Please, Sign In to add comment