Let n=5
, then for matrix
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
then sum of diagonal elements is:
=1+17+25+21+9+5+19+23+13
Sum for n=15
?
One way is to make the spiral matrix and then by a loop, we get the answer, but its time and space complexity is large.
Like this https://www.geeksforgeeks.org/sum-diagonals-spiral-odd-order-square-matrix/ but the problem here starts 1 from the center.
Based on Bob_'s answer, Here is a recursive code in CPP as requested by OP
try it out here https://rextester.com/FLJD46264
Python - https://rextester.com/MDMV32855