File tree 1 file changed +56
-0
lines changed
Difficulty: Easy/Third largest element
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ // { Driver Code Starts
2
+ // Initial template for C++
3
+
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+
7
+
8
+ // } Driver Code Ends
9
+
10
+ class Solution {
11
+ public:
12
+ int thirdLargest (vector<int > &arr) {
13
+ // Your code here
14
+ int first = -1 , second = -1 , third = -1 ;
15
+ for (int num: arr){
16
+ if (num >= first){
17
+ third = second;
18
+ second = first;
19
+ first = num;
20
+ }
21
+ else if (num >= second){
22
+ third = second;
23
+ second = num;
24
+ }else if (num >= third){
25
+ third = num;
26
+ }
27
+ }
28
+ return third;
29
+ }
30
+ };
31
+
32
+
33
+ // { Driver Code Starts.
34
+ int main () {
35
+ int t;
36
+ cin >> t;
37
+ cin.ignore ();
38
+ while (t--) {
39
+ vector<int > arr;
40
+ string input;
41
+ getline (cin, input); // Read the entire line for the array elements
42
+ stringstream ss (input);
43
+ int number;
44
+ while (ss >> number) {
45
+ arr.push_back (number);
46
+ }
47
+
48
+ Solution ob;
49
+ cout << ob.thirdLargest (arr) << endl;
50
+ cout << " ~" << endl;
51
+ }
52
+
53
+ return 0 ;
54
+ }
55
+
56
+ // } Driver Code Ends
You can’t perform that action at this time.
0 commit comments