-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.cpp
146 lines (119 loc) · 3.31 KB
/
Test.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Mock Test - Amazon
#include <bits/stdc++.h>
using namespace std ;
long getMinValue(vector<long> power, long armor) {
long minHealthRequired = 1;
long maxHealth = INT_MIN;
for(auto &it: power) {
maxHealth = max(maxHealth, it);
minHealthRequired += it;
}
minHealthRequired -= min(armor, maxHealth);
return minHealthRequired;
}
int findLongestLength(const string& fullString) {
int n = fullString.size();
if (n <= 1) return 0;
unordered_map<char, int> totalFrequency;
for (char c : fullString) {
totalFrequency[c]++;
}
int maxLength = 0;
for (int i = 0; i < n; ++i) {
unordered_map<char, int> substringFrequency;
unordered_set<char> uniqueChars;
bool isSelfSufficient = true;
for (int j = i; j < n; ++j) {
char currentChar = fullString[j];
substringFrequency[currentChar]++;
uniqueChars.insert(currentChar);
for (char c : uniqueChars) {
if (substringFrequency[c] > 0 && totalFrequency[c] != substringFrequency[c]) {
isSelfSufficient = false;
break;
}
}
if (isSelfSufficient && (j - i + 1 < n)) {
maxLength = max(maxLength, j - i + 1);
} else {
break;
}
}
}
return maxLength;
}
int findMaxZeroes(vector<int> arr) {
int n = arr.size();
vector<int> leftMin(n);
int mini = INT_MAX;
for(int i = 0; i < n; ++i) {
mini = min(arr[i], mini);
leftMin[i] = mini;
}
int count = 0;
for(int i = 0; i < n; ++i) {
count += (arr[i] == leftMin[i]);
}
return count;
}
int getOutlierValue(vector<int> arr) {
map<int, int> mp;
int sum = 0;
int sol = 0;
for(int i=0; i<arr.size(); i++){
sum += arr[i];
mp[arr[i]]++;
}
for(int i=0; i<arr.size(); i++)
{
if(mp.find((sum-arr[i])/2)!=mp.end() && arr[i]!=(sum-arr[i])/2 && (sum-arr[i])%2==0) sol = max(sol, arr[i]);
}
return sol;
}
auto init = []() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 'c';
}();
int maxSubstringLength(string s) {
vector<int> first(26, -1);
vector<int> last(26);
int n = s.length();
for (int i = 0; i < n; ++i) {
int j = s[i] - 'a';
if (first[j] == -1) {
first[j] = i;
}
last[j] = i;
}
int ans = -1;
for (int k = 0; k < 26; ++k) {
int i = first[k];
if (i == -1) {
continue;
}
int mx = last[k];
for (int j = i; j < n; ++j) {
int a = first[s[j] - 'a'];
int b = last[s[j] - 'a'];
if (a < i) {
break;
}
mx = max(mx, b);
if (mx == j && j - i + 1 < n) {
ans = max(ans, j - i + 1);
}
}
}
if (ans == -1) ans = 0;
return ans;
}
int main () {
vector<long> arr = {1, 2, 3};
// cout << getOutlierValue(arr);
string input = "amazonservices";
// cout << getMinValue(arr, 1);
cout << maxSubstringLength(input);
return 0;
}