# Help me in solving MAXIMALEXP problem

### My issue

can anyone tell where am i wrong or for which test case.

### My code

``````#include <iostream>

int main() {
int t;
std::cin>>t;
while(t--)
{
int N, K;
std::cin >> N >> K;

int max_X = -1, max_value = -1;

for (int X = 0; X <= N; ++X) {
int value = (X % K) * ((N - X) % K);
if (value > max_value) {
max_value = value;
max_X = X;
}
}

std::cout << "The value of X for maximum F(X) is: " << max_X << std::endl;
}
return 0;
}

``````

Problem Link: MAXIMALEXP Problem - CodeChef

@shubham_731
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';
}
}
``````

how someone can observe that if(k>(n+1)/2) we have to print (n+1)/2 similarly the third if statement is there any approach that you can explain.
Also explain me that for which test case

the above code is satisfied.

@shubham_731
The thing is when u are taking mod with K the maximum u can get is k-1.
So the ideal case would be getting (K-1)*(K-1).
Now when k>n
U will get the maximum value at (N%k)/2; (which is the middle value of n).
and if n>=k
then u have to get the new middle value for which u will get the maximum answer.
and for this case if n%k is !=k-1.
then U can add (k+1)/2 to the (N%k)/2 to get to the new middle value for which u will get the maximum answer.
Its not quite intuitive .
U have to take several smaller test cases considering both cases when k>n and when n>=k .
only then U can make these observations.
i’ll suggest to try for several smaller test cases in rough to figure out what’s happening and how u are getting maximum answer in middle values.