Help for recent cook off problem (BNSONSTR)

why this solution is giving TLE i have done almost same thing as everyone else problem link

ll change(string &s,ll d){
	vector<ll> vec(d);
	ll maxi_ind=0;
	ll maxi=0;
	for(int i=0;i<(ll)s.size();i++){
		if(s[i]=='1'){
			vec[i%d]++;
		}
		if(vec[i%d]>maxi){
			maxi=vec[i%d];
			maxi_ind=i%d;
		}
	}

	ll ans=0;
	for(int i=0;i<(ll)s.size();i++){
		if(i-maxi_ind>=0 and (i-maxi_ind)%d==0){
			if(s[i]=='0') ans++;
		}else if(s[i]=='1') ans++;
	}
	
	return ans;
}


string solve(){
	
	ll n;
	cin>>n;
	
	string s;
	cin>>s;
	
	vector<ll> root;
	ll num=s.size();
	for(ll i=1;i*i<=num;i++){
		if(num%i==0){
			root.push_back(i);
			root.push_back(num/i);
		}
	}
	
	ll ans=INF;
	for(auto x:root){
		ans=min(ans,change(s,x));
	}
	
	ret(ans);
	
	
ret("");	
}

why this solution is giving TLE i have done almost same thing as everyone else problem link

Paste the code which could be compiled , I have a feel that this code will Also give WA , as you are changing maxi_index many time

i have changed the code in the blog… submission link

Just passed on the edge :slight_smile:
AC
Change LL from long long to int and reduced

to
if(i%d==maxi_ind)

ohhhhhhh man i was not knowing that long long is that much slow :open_mouth:

It is not too slow , but yess it takes a bit more time than int as it has 64 bits rather than 32 bits in memory.

1 Like

Can you pls check my code also, it has the same logic, but giving WA. I have changed long long to int in template, which removed TLE, but unfortunately got WA.

Link to submission.

It fails on
1
5
00000

Your output : 0
Correct Output : 1

1 Like

Aree bro, thanks a lot! :slight_smile:
Didn’t thought of such case, damn.

1 Like