My testcase 2 and 4 are showing wrong answer. The code is following :
your approach is wrong try for 3^2