Optimize slow code

When I ran three loops for few n (size), I noticed that all combinations are not to be checked . Hence I reduced almost 2 loops but im getting wrong answer.

Thanks

Your logic seems incorrect to me…

The logic is to find maximum sum of exactly three numbers of array y such that the values of x(i),x(j),x(k) are unique to each other where i,j,k are indices of the numbers in array y…
Hence logic is to find three unique numbers of x such that sum of corresponding y are max…
I have two solutions in my mind

Solution 1)

Click to view

map + vector pair + finding max 3 elements from an array(can be done with sorting as well as without sorting)…
O(N) solution (without sorting).
Can be O(nlog_2 n) (with sorting)

Solution 2)

Click to view

vector pair + sorting + finding max 3 elements such that corresponding x are unique(greedy approach)… which is comparatively slower than both approaches of solution 1…

Feel free to ask queries and correct me if I am wrong…

I can explain detailed solution for both approach given by me… Feel free to ask…