-
Notifications
You must be signed in to change notification settings - Fork 0
/
Largest Pair Sum.cpp
49 lines (46 loc) · 1.04 KB
/
Largest Pair Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*class Solution {
public:
int pairsum(vector<int> &arr) {
int n=arr.size();
int maxSum=0; //BRUTE FORCE APPROACH
sort(arr.begin(),arr.end());
for(int i=0;i<n;i++){
maxSum=arr[i-1]+arr[i];
}
return maxSum;
}
};
*/
/*class Solution {
public:
int pairsum(vector<int> &arr) {
// code here
priority_queue<int>maxHeap;
for(int i=0;i<arr.size();i++){ //PRIORITY QUEUE APPROACH
maxHeap.push(arr[i]);
}
int a=maxHeap.top();
maxHeap.pop();
int b=maxHeap.top();
maxHeap.pop();
return a+b;
}
};
*/
class Solution {
public:
int pairsum(vector<int> &arr) {
int n=arr.size();
int l1=INT_MIN;
int l2=INT_MIN; //OPTIMIZED APPROACH
for(int i=0;i<n;i++){
if(arr[i]>l1){
l2=l1;
l1=arr[i];
}else if(arr[i]>l2){
l2=arr[i];
}
}
return l1+l2;
}
};