CJCoding With Joseph
15free / daySign inCreate account

Second Largest with Iterator Arithmetic

Write a function int secondLargest(vector<int> v) that returns the second-largest value in v. The vector is passed BY VALUE, so sort it ascending in place with sort, then return the element two positions before end(). You may assume the vector always has at least 2 elements. Duplicates count as separate positions: for {4, 9, 9} the answer is 9.

The test prints the returned value on its own line. For {3, 7, 9, 1} it prints 7.

Do NOT write a main function.

Expected Output:

7
2

= newline. The output ends with a newline — print one after the last line.

Hints2 available
Topics:
STL ContainersIterators
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.