-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complement.cpp
68 lines (56 loc) · 1.38 KB
/
Complement.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
vector<int> findRange(string str, int n) {
// code here
int i=0;
int j=0;
unordered_map<char ,int> mp;
int maxSub = 0;
vector<int> ans = {-1};
int temp = 0;
while(j < n){
mp[str[j]]++;
if(mp['0'] >= mp['1']){
temp = mp['0'] - mp['1'];
if(temp > maxSub){
maxSub = temp;
ans = {i+1, j+1};
}
j++;
}else{
while(mp['0'] < mp['1']){
mp[str[i]]--;
if(mp[str[i]] == 0) mp.erase(str[i]);
i++;
}
j++;
}
}
if(ans[0] == -1) return {-1};
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
Solution ob;
auto ans = ob.findRange(s, n);
if (ans.size() == 1) {
cout << ans[0] << "\n";
} else {
cout << ans[0] << " " << ans[1] << "\n";
}
}
return 0;
}
// } Driver Code Ends