A Blunder That Stopped Me From Being a Pupil (sqrt DANGEROUS!!!)

Revision en2, by ilove_sundarKanya, 2024-12-03 11:44:11

In today’s Div. 2 contest, I encountered a frustrating issue that taught me the importance of precision in competitive programming.

The problem logic seemed straightforward: identify odd perfect squares. Confidently, I implemented the logic in my code editor (VS Code), tested a few cases locally, and it seemed flawless. However, when I submitted the solution during the contest, it gave Wrong Answer on Test 1 repeatedly. Problem:

I couldn’t accept that such an easy problem was slipping away. Frustration kicked in as I spent over 40 minutes debugging and even tried different C++ standards like C++17, C++20, and C++23, only to face the same result: Wrong Answer on Test 1. Disheartened, I shifted my focus to solving other problems, managing to complete Question B and Question C before circling back with just 30 minutes remaining.

After some reflection and experimenting, I finally pinpointed the issue:

Incorrect Code

It turns out that floating-point arithmetic caused the issue. sqrt(n) * sqrt(n) does not always yield exact results due to precision errors in floating-point operations.

I replaced it with a safer and integer-based alternative:

Correct Code

This resolved the issue instantly, but by then, the damage was done. The time lost on debugging and the rank I could have earned were irrecoverable.

I hope whoever reads this avoids such a mistake in the future.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English ilove_sundarKanya 2024-12-03 11:44:43 4 Tiny change: 'eatedly.\n[Problem' -> 'eatedly.\n\n[Problem'
en2 English ilove_sundarKanya 2024-12-03 11:44:11 53
en1 English ilove_sundarKanya 2024-12-03 11:43:00 1897 Initial revision (published)