Help me in solving MAXIMALEXP problem

My issue

include
using namespace std;

int main(){
int t;
cin>>t;
while(t–){
int n,k,b;
cin>>n>>k;
int max,a=0;
for (int i=0;i<=n;i++){
a=(i%k)*((n-i)%k);
if (max<a){
max=a;
b=i;
}
}
cout<<b<<endl;

}
return 0;

}
please help me finding error in this ques maximal expression running perefectly for 2 test case but choking in 3rd one

My code

#include <iostream>
using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
	    int n,k,b;
	    cin>>n>>k;
	    int max,a=0;
	    for (int i=0;i<=n;i++){
	        a=(i%k)*((n-i)%k);
	        if (max<a){
	            max=a;
	            b=i;
	        }
	    }
	    cout<<b<<endl;
	    
	}
	return 0;
}

Problem Link: MAXIMALEXP Problem - CodeChef

no, it is correct, if you have seen the test case 3, you will find it is written that 1,4,7 all are correct, its just the runtime error which happened with this code, as i wrote the same logic and code, so i am getting the runtime error and i don’t know how to resolve it

you should initialize b and max to fix your code.
However regarding the expression x^2 - a*x you may come to way to avoid TLE for bigger n.

i intialized them just above the for loop

Nope, you didn’t initialize either of them.
int \ max,a=0;
… and …
int \ max=0, a=0;
→ spot the difference :wink: