Find missing first positive number in the sequence in c#- Learn how to find the missing first positive number among the sequence of numbers from 1 to n with example Programs.
For Example following are the numbers from -3 to 6 as -3,-1,0,1,2,3,5,6.
The first positve missing number in the above sequence is 4.
C# Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Program { static void Main(string[] args) { //array to find the missing number between -3 and 10 // Simplicity, We will take number -3 to 10 i where positive Number 4 is missing in the sequence. int[] arr = {-3, -1, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10 }; Array.Sort(arr); for (int i = 1; i < arr.Length; i++) { if (arr[i]>0) { if(arr[i]-1 !=arr[i-1]) { Console.WriteLine("first missing positive number : {0}", arr[i] - 1); break; } } } Console.ReadLine(); } } |