Posts

Showing posts with the label second max element

Second max element in a array

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SecMax { class Program { static void Main(string[] args) { int[] arr = { 10000, 2000, 300, 200, 1, 50, 56, 72 }; //assumption: no 2 numbers will be equal. That part is not covered if (arr[0] < arr[1]) { int temp = arr[0]; arr[0] = arr[1]; arr[1] = temp; } for (int i = 2; i <= arr.Length - 1; i++) { if (arr[i] >= arr[0]) { arr[0] = arr[i]; } else if (arr[i] >= arr[1]) { arr[1] = arr[i]; } } Console.WriteLine(arr[0]); Console.WriteLine(arr[1]); Console.ReadLine(); } } }