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: < ? php function getCount ( $ M , $ N ) { $ count = 0 ; if ( $ M == 1 ) return $ N ; if ( $ N == 1 ) return $ M ; if ( $ N > $ M ) { for ( $ i = 1 ; $ i <= $ M ; $ i ++ ) { $ numerator = $ N * $ i - $ N + $ M - $ i ; $ denominator = $ M - 1 ; if ( $ numerator % $ denominator == 0 ) { $ j = $ numerator / $ denominator ; if ( $ j >= 1 and $ j <= $ N ) $ count ++ ; } } } else { for ( $ j = 1 ; $ j <= $ N ; $ j ++ ) { $ numerator = $ M * $ j - $ M + $ N - $ j ; $ denominator = $ N - 1 ; if ( $ numerator % $ denominator == 0 ) { $ i = $ numerator / $ denominator ; if ( $ i >= 1 and $ i <= $ M ) $ count ++ ; } } } return $ count ; } $ M = 3 ; $ N = 5 ; echo getCount ( $ M , $ N ) ; ? >