Click here to Skip to main content
15,076,991 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.

int f(int[ ] a)

if the input array is	return
{1, 2, 3, 4}	3
{{4, 1, 2, 3}}	3
{1, 1, 2, 2}	1
{1, 1}	-1
{1}	-1
{}	-1

What I have tried:

what will be yhe answer of this question with output
Updated 2-Oct-17 22:01pm

Take a sheet of paper and a pencil. And give it a try, find a mechanical way to solve the problem, that way is the algorithm you are looking for.
Advice: Start with code that search for largest integer.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements."[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
I would suggest sorting[^] the array in descending order first and then getting the value in array[1].
Checking if -1 needs to be returned would need a few simple if constructs.
You have C++, you have the power of the standard library:
  • sort the array in descending order (see std::sort[^]).
  • remove adjacent duplicate items (see std::unique[^]).
  • return the 2nd item of the array if it exist, otherwise return -1.
Patrice T 3-Oct-17 5:00am
Resorting to 'sort' just to find the second largest number look like a lot of work for a rather simple thing.
CPallini 3-Oct-17 5:39am
It depends on context, that is the expected size of the input array. For large arrays , std::unique would be overkilling as well.
«_Superman_» 4-Oct-17 0:46am
I would disagree.
std::sort is there to use.
I believe this would be more optimized rather than iterating over a loop.
CPallini 4-Oct-17 4:17am
Well, no. Iterating would require O(N) while, for instance, quick sort average case is O(N log N).
Patrice T 4-Oct-17 4:28am
Don't believe, experiment different solutions and see which one is better.
Finding the second largest value need to iterate once other the list, and it is faster than any other solution, just experiment.
«_Superman_» 4-Oct-17 4:34am
I agree. For this case iterating once over the list would indeed be the fastest.
«_Superman_» 4-Oct-17 4:23am
I would still prefer sort to having if conditions inside a loop.
Patrice T 4-Oct-17 4:38am
One can expect a sort to hide a few ifs inside a few loops.
«_Superman_» 4-Oct-17 4:43am
Definitely yes. But std::sort is tried and tested. Who knows how it is going to be optimized in future. Having said that, after experimenting, if the performance makes a huge difference, then would definitely get rid of std::sort.
CPallini 4-Oct-17 4:48am
That's exactly the point, in my opinion. For small arrays I would definitely use sort and unique.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900