text: Number of positions with Same address in row major and column major order | Returns count of required positions ; horizontal 1D array ; vertical 1D array ; iterating for all possible i ; checking if j is integer ; checking if j lies b / w 1 to N ; iterating for all possible j ; checking if i is integer ; checking if i lies b / w 1 to M ; Driver Code
code: function getCount ( M , N ) { let count = 0 ; if ( M == 1 ) return N ; if ( N == 1 ) return M ; if ( N > M ) { for ( let i = 1 ; i <= M ; i ++ ) { let numerator = N * i - N + M - i ; let denominator = M - 1 ; if ( numerator % denominator == 0 ) { let j = parseInt ( numerator / denominator , 10 ) ; if ( j >= 1 && j <= N ) count ++ ; } } } else { for ( let j = 1 ; j <= N ; j ++ ) { let numerator = M * j - M + N - j ; let denominator = N - 1 ; if ( numerator % denominator == 0 ) { let i = parseInt ( numerator / denominator , 10 ) ; if ( i >= 1 && i <= M ) count ++ ; } } } return count ; } let M = 3 , N = 5 ; document . write ( getCount ( M , N ) ) ;