Question: Design and implement a (n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element

Design and implement a (n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You should NOT make any assumption regarding the values of the elements and their distribution in the array.

Show the working of the algorithm with the following two types of example arrays: (a) An example array of 10 integers that need to have the largest integer appearing at least twice. (b) An example array with the largest integer and second-largest integer as respectively the first

Please put comments in the code (C++) and prove time complexity is theta(n)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!