Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace HelloWorld
- {
- class Program
- {
- static void Main(string[] args)
- {
- // These are the arrays with the numbers:
- int[] array1 = { 2, 4, 3, 201, 1003, 1, 6, 6, 7, -20, 10000, -30, 200303 };
- int[] array2 = { 10, -2, 1000, 5, -300, -200, -1000, 100, 10000, -2042, 32021 };
- // These three methods make array2 bigger to hold array1, combines both arrays into array2, and then the method sorts array2 numerically.
- Array.Resize(ref array2, array2.Length + array1.Length + 10);
- Array.Copy(array1, 0, array2, array1.Length, array1.Length);
- Array.Sort(array2);
- // Eliminate the edge case of repeated values by calling a linq function to eliminate duplicates.
- IEnumerable<int> array = array2.Distinct();
- // x is an integer that holds the current checked value between 1 and 100,000. In my code, it can only go up.
- int x = 1;
- // Iterates through every element in array2, which contains both array1 and array2.
- foreach (int i in array)
- {
- // i being the current element in array2, this line skips over every element that is below 1.
- if (i < 1) continue;
- // if i does not equal the current checked value, then we have the answer. It prints the answer and leaves the program.
- if (i != x)
- {
- Console.WriteLine(x);
- return;
- }
- // Increase x and check the next number in the array.
- x++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement