Find the element at specified index in a Spiral Matrix | Function to the find element at ( i , j ) index ; Driver Code ; Function Call
function findInGrid ( i , j ) { if ( i == j ) return ( i * i - ( i - 1 ) ) ; else if ( i > j ) { if ( i % 2 == 0 ) return i * i - ( j - 1 ) ; else return ( i - 1 ) * ( i - 1 ) + 1 + ( j - 1 ) ; } else { if ( j % 2 == 0 ) return ( j - 1 ) * ( j - 1 ) + 1 + ( i - 1 ) ; else return j * j - ( i - 1 ) ; } } let i = 3 , j = 4 ; document . write ( findInGrid ( i , j ) ) ;