Please SUBSCRIBE❤ CODE : // User function template for C++ class Solution { public: // Function returns the second // largest elements int getSecondLargest(vector &arr) { //Dilemmas Decoder if (arr.size() < 2) { return -1; } int largest = arr[0]; int secondLargest = -1; for (int i = 1; i < arr.size(); ++i) { if (arr[i] > largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != largest) { secondLargest = arr[i]; } } return secondLargest; } };
Please SUBSCRIBE❤
CODE :
// User function template for C++
class Solution {
public:
// Function returns the second
// largest elements
int getSecondLargest(vector &arr) {
//Dilemmas Decoder
if (arr.size() < 2) {
return -1;
}
int largest = arr[0];
int secondLargest = -1;
for (int i = 1; i < arr.size(); ++i) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
return secondLargest;
}
};
ai thumbnail
Please Subscribe