forked from black-shadows/InterviewBit-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlip.cpp
More file actions
63 lines (59 loc) · 1.57 KB
/
Flip.cpp
File metadata and controls
63 lines (59 loc) · 1.57 KB
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
// https://www.interviewbit.com/problems/flip/
vector<int> Solution::flip(string A) {
int check = 0, another_check = 0;
int left, right, left_final, right_final;
int temp1 = 0, max1 = 0;
char c;
// cout << A.size();
for(int i = 0; i < A.size(); i++){
// char c = A.at(i);
c = A[i];
check++;
if(check == 1){
left = i;
}
right = i;
if(c == '1'){
temp1--;
}
if(c == '0'){
temp1++;
}
if(temp1 >= 0){
another_check++;
if(temp1 > max1){
max1 = temp1;
left_final = left;
right_final = right;
}
else if(temp1 == max1){
if(left == left_final){
if(right > right_final){
// nothing
}
else if(right_final > right){
right_final = right;
}
}
else if(left > left_final){
// nothing
}
else{
// left < left_final
left_final = left;
right_final = right;
}
}
}
else{
check = 0;
temp1 = 0;
}
}
vector<int> sol;
if(another_check != 0){
sol.push_back(left_final+1);
sol.push_back(right_final+1);
}
return sol;
}