-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFebruary-23.cpp
74 lines (59 loc) · 1.77 KB
/
February-23.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<int> nextLargerElement(vector<int>& arr) {
stack<int> s;
vector<int> res(arr.size());
for (int i = arr.size() - 1; i >= 0; i--) {
while (!s.empty() && s.top() <= arr[i]) s.pop();
res[i] = s.empty() ? -1 : s.top();
s.push(arr[i]);
}
return res;
}
};
2)
class Solution {
public:
vector<int> nextLargerElement(vector<int>& arr) {
list<int> indices;
vector<int> res(arr.size(), -1);
for (int i = arr.size() - 1; i >= 0; i--) {
while (!indices.empty() && arr[indices.back()] <= arr[i]) indices.pop_back();
if (!indices.empty()) res[i] = arr[indices.back()];
indices.push_back(i);
}
return res;
}
};
//{ Driver Code Starts.
int main() {
int t; // Number of test cases
cin >> t;
cin.ignore(); // Ignore the newline after reading t
while (t--) {
vector<int> a;
string input;
// Reading the entire input line for the array
getline(cin, input);
stringstream ss(input);
int num;
while (ss >> num)
a.push_back(num); // Read the array elements from input string
Solution obj;
vector<int> result = obj.nextLargerElement(a);
// Print the result in the required format
for (int i = 0; i < result.size(); i++) {
if (i != 0)
cout << " ";
cout << result[i];
}
cout << endl; // Ensure new line after each test case output
cout << "~" << endl; // Ensure new line after each test case output
}
return 0;
}
// } Driver Code Ends