Problem in finding the right root of the quadratic equation

There is a cuboid. Assume that the width of the base is X. Then the Length of the base should not exceed (A−X) and the Height should not exceed (B−X). Also The Volume should be maximum. you are given the two integers A and B,find an integer X such that the Volume is maximum.find minimum X.

Note: All dimensions should be strictly positive integers.

Constraints
1≤T≤500000(Number of testcases)
2≤A, B≤50000(Integers only)
output in a single line the value of X and the maximum volume.

So, it seems fairly straightforward that we have to differentiate (A-X)*(B-X)*(X) and equal to 0 will give us the quadratic equation, and we have to find the minimum positive X and calculate the volume. But, my code is giving WA for some cases.
Last edited on
in your code , make a condition that none of your dimension for the selected ans variable is not becoming negative.
@zyan1zyan I added the condition in getting two positive values, that the minimum should give strictly positive dimensions, am still getting WA :(
Last edited on
You say a and b can be as high as 50000 and at one point you are calculating a*b. If they are both 50000 then the result is just beyond the range of a 32-bit signed int. Make a and b long long.

Last edited on
@tpb thanks, its resolved now.
Topic archived. No new replies allowed.