hi, let Avik connect you.


** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.

** You can check all the posts on the Posts page.

** More about me in the About Me section.

One iteration


  • Approach:

    1. Find out the first max and second max from the list.

    2. Add them and return them.


  • Time and Space Complexity:

      • Time Complexity: O(N)

      • Space Complexity: O(1)


Code [C++]

#include <bits/stdc++.h>

int oneIteration(vector<int> A)

{

int fMax = INT_MIN, sMax = INT_MIN;

for(int i=0; i<A.size(); i++){

if(A[i] >= fMax){

sMax = fMax;

fMax = A[i];

}

else if(A[i] > sMax){

sMax = A[i];

}

}

return (fMax + sMax);

}