Rearranging gives . Thus the equality holds exactly when or .
The main solution checks these two conditions. The naive solution directly computes and compares it with . Both take time and extra space per case and total time. Both methods work under the full constraints.
Since and , even signed 32-bit arithmetic is sufficient. Do not divide both sides by without checking it, since may be zero.
Solution written by GPT