Help me in solving MAXIMALEXP problem

My issue

it show wrong answer on this approach.
k>=n
x<K and n-x<k so their product after module =x*(n-x)
k<n
running a loop for finding max value at each value of x in else case.

My code

``````#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
int t;cin>>t;
while(t--){
int n,k;
cin>>n>>k;
if(k==1||k==2)
cout<<0<<endl;

else if(n<=k)
n&1?cout<<((n+1)/2)<<endl:cout<<(n/2)<<endl;

else{
int x=0,maxi=0,ans=0;
while(x<=n){
if(maxi<((x%k)*((n-x)%k))){
ans=x;
maxi=(x%k)*((n-x)%k);
}
x++;
}
cout<<ans<<endl;
}

}

return 0;
}
``````

Problem Link: MAXIMALEXP Problem - CodeChef

due to high constraints u canâ€™t loop through to find the maximum value.
just observe some smaller test cases .
U will get the pattern
refer the following solution for better understanding .

``````
#include <iostream>
using namespace std;

int main()
{
int t;
cin >> t;
while(t--)
{
int n, k;
cin >> n >> k;
if (k == 1)
{
cout << "0\n";
continue;
}
if (k > (n + 1) / 2)
{
cout << (n + 1) / 2 << '\n';
continue;
}
if (n % k == 0 || n % k == k - 1)
{
cout << k / 2 << '\n';
continue;
}
int l = n % k + 1, r = k - 1;
cout << l + (r - l) / 2 << '\n';
}
}``````