-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimal_shifts.cpp
More file actions
77 lines (74 loc) · 1.02 KB
/
optimal_shifts.cpp
File metadata and controls
77 lines (74 loc) · 1.02 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
void solve(string &s)
{
int n = s.size();
int maximum = 0;
int check = 0;
if(s[n-1]=='1') //O(N) Approach
{
for(int i=n-1;i>=0;i--)
{
if(s[i]=='1')
{
check = 0;
}
else
{
check++;
maximum = max(maximum,check);
}
}
cout<<maximum<<endl;
}
else // O(2N) Approach
{
int locate;
for(int i = n-2;i>=0;i--)
{
if(s[i]=='1')
{
locate = i;
break;
}
}
int i = (locate - 1 + n) % n;
while(i!=locate)
{
if(s[i]=='1')
{
check = 0;
}
else
{
check++;
maximum = max(maximum,check);
}
i--;
if(i<0)
if (i < 0) i = n - 1;
}
cout<<maximum<<endl;
}
return;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
solve(s);
}
return 0;
}