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
Problem: One iteration
Problem Link: www.codingninjas.com/codestudio/problems/one-iteration_3116723
Companies Asked: Walmart, Amazon.
Approach:
Find out the first max and second max from the list.
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);
}