forked from TusharKukra/Hacktoberfest2021-EXCLUDED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreatesElement.cpp
51 lines (41 loc) · 984 Bytes
/
GreatesElement.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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
std::vector<int> replaceElements(std::vector<int> &arr) {
// Solution with double loop
//
int max = 0;
for (int i = 0; i < arr.size(); i++) {
max = 0;
for (int j = i + 1; j < arr.size(); j++) {
if (arr[j] > max) {
max = arr[j];
}
}
arr[i] = max;
}
arr[arr.size() - 1] = -1;
return arr;
// Alternate solution
/*
// alternate solution using *max_element
for(int i = 0; i < arr.size(); i++){
if(arr.begin() + 1 + i < arr.end()){
int g = *max_element(arr.begin() + i + 1, arr.end());
arr[i] = g;
}
}
arr[arr.size() - 1] = -1;
return arr;
*/
}
};
int main() {
std::vector<int> arr = {0, 1, 2, 3};
Solution s;
s.replaceElements(arr);
for (auto i : arr)
std::cout << i << " ";
}