CSES - Number Spiral

I was solving CSES Number Spiral - CSES - Number Spiral
My solution - https://cses.fi/paste/4af0cd10fc8228471690ba/

For some inputs, The code is working for others it is failing. I am unable to find out the reason.
Explanation for code:
My approach is to find out the diagonal element first (m,m). For eg, 3,4 will lie in the column of 4,4 and 6,5 will lie in the row of of 6,6 and so on.
After that using the bool variable (col), I find out if the required element lies in the same column or the same row.
col_inc is used to find if the values are increasing in the column or decreasing in the column ( starting from row 0) and vice versa for row.

After this I just print the values using the formulas - ele -/+ (m -x/y).

Can you please help me find out my mistake? (I know this is very complex approach to a simple solution but I want to know where I made a mistake)

Thank you in advance.

Input:
170550340 943050741
121998376 943430501
689913499 770079066
586095107 933655238

Expected Output:
889344699930098742
890061110095112626
593021767041187724
871712102163621276
495633841728043220

My output:
889344699930098749
890061110095112665
593021767041187688
871712102163621240
495633841728043220

I dont know much about c++ but instead of using pow i just used m*m and it worked

1 Like