Finding the highest values a and b from an array
What you are doing is just normal search for a max pair using lexicographical ordering.

The issue with your code is that, it is linear O(n). While for a single use, this might be the best way, if you have to do it repeatedly, it would be better to build a priority queue [O(n)] with the comparison operator defined as pair compare, and then extract the top. (Even better if you can build the priority queue as you receive the elements.)

The pair comparison is derived directly from the definition 

(a,b) ≤ (a′,b′) if and only if a &lt; a′ or (a = a′ and b ≤ b′).


However you have mentioned the constraint that the loop shouldn't be changed. Is there a reason for that?