...
/Solution Review: Finding the Second Maximum Value in an Array
Solution Review: Finding the Second Maximum Value in an Array
This review provides a detailed analysis of the different ways to solve the "Finding the Second Maximum Value in an Array" challenge.
Solution #1: Traversing the array twice
Press + to interact
namespace chapter_2{class Challenge7{//Find Second Maximum Value in an Arraystatic int findSecondMaximum(int []arr, int size){int max = Int32.MinValue;int secondmax = Int32.MinValue;// Keep track of the maximum value, whenever the value at an array index is greater// than the current maximum value, then make that max value second max value and// make that index value maximum valuefor (int i = 0; i < size; i++){if (arr[i] > max)max = arr[i];}//end of for-loopfor (int i = 0; i < size; i++){if (arr[i] > secondmax && arr[i] < max)secondmax = arr[i];}//end of for-loopreturn secondmax;}static void Main(string[] args){int size = 5;int []arr = { -2, -33, -10, -33, -456 };Console.Write("Array: ");for (int i = 0; i < size; i++)Console.Write(arr[i] + " ");Console.WriteLine("");int secMax = findSecondMaximum(arr, size);Console.WriteLine("Second maximum: " + secMax);return ;}}}
Traverse the array twice. In the first traversal, you find the maximum element. In the second traversal, you find the greatest element less than the element obtained in the first traversal.
Time complexity
The time complexity of the solution is in ...