Advertisement
Guest User

Untitled

a guest
Aug 1st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. // The problem to solve: Given two arrays of integers, assuming values between 1 and 100,000, find the lowest nonzero nonnegative number that does not exist in either array, and print it.
  2.  
  3. using System;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6.  
  7. namespace HelloWorld
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // These are the arrays with the numbers:
  14. int[] array1 = { 2, 4, 3, 201, 1003, 1, 6, 6, 7, -20, 10000, -30, 200303 };
  15. int[] array2 = { 10, -2, 1000, 5, -300, -200, -1000, 100, 10000, -2042, 32021 };
  16.  
  17. // These three methods make array2 bigger to hold array1, combines both arrays into array2, and then the method sorts array2 numerically.
  18. Array.Resize(ref array2, array2.Length + array1.Length + 10);
  19. Array.Copy(array1, 0, array2, array1.Length, array1.Length);
  20. Array.Sort(array2);
  21.  
  22. // Eliminate the edge case of repeated values by calling a linq function to eliminate duplicates.
  23. IEnumerable<int> array = array2.Distinct();
  24.  
  25. // x is an integer that holds the current checked value between 1 and 100,000. In my code, it can only go up.
  26. int x = 1;
  27.  
  28. // Iterates through every element in array2, which contains both array1 and array2.
  29. foreach (int i in array)
  30. {
  31. // i being the current element in array2, this line skips over every element that is below 1.
  32. if (i < 1) continue;
  33.  
  34. // if i does not equal the current checked value, then we have the answer. It prints the answer and leaves the program.
  35. if (i != x)
  36. {
  37. Console.WriteLine(x);
  38. return;
  39. }
  40.  
  41. // Increase x and check the next number in the array.
  42. x++;
  43.  
  44. }
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement