Number of positions with Same address in row major and column major order
Last Updated :
20 Oct, 2022
Given a 2D array of size M x N. Calculate count of positions in 2D array where address as per row-major order equals to address as per column-major order.
Examples:
Input : 3 5
Output : 3
Row major address is same as column major for following i, j
pairs (1, 1), (2, 3) & (3, 5)
Input : 4 4
Output : 4
Let’s consider element with index i, j
Row major address = B + w * (N * (i-1) + j-1)
Column major address = B + w * (M * (j-1) + i-1)
B: Base address of the array
w: Size of each element of the array
Equating both addresses, we get
B + w * (N * (i-1) + j-1) = B + w * (M * (j-1) + i-1)
N * (i-1) + j = M * (j-1) + i
N*i - N + j = M*j - M + i
M*j - j = N*i - N + M - i
(M-1) * j = N*i - N + M - i
j = (N*i - N + M - i)/(M-1) - (Eq. 1)
Similarly
i = (M*j - M + N - j)/(N-1) - (Eq. 2)
Now we have established a relation between i and j
Iterate for all possible i and find corresponding j
If j comes out to be an integer in the range 1 to N,
increment the counter.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int getCount( int M, int N)
{
int count = 0;
if (M == 1)
return N;
if (N == 1)
return M;
if (N > M) {
for ( int i = 1; i <= M; i++) {
int numerator = N * i - N + M - i;
int denominator = M - 1;
if (numerator % denominator == 0) {
int j = numerator / denominator;
if (j >= 1 && j <= N)
count++;
}
}
}
else {
for ( int j = 1; j <= N; j++) {
int numerator = M * j - M + N - j;
int denominator = N - 1;
if (numerator % denominator == 0) {
int i = numerator / denominator;
if (i >= 1 && i <= M)
count++;
}
}
}
return count;
}
int main()
{
int M = 3, N = 5;
cout << getCount(M, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int getCount( int M, int N)
{
int count = 0 ;
if (M == 1 )
return N;
if (N == 1 )
return M;
if (N > M) {
for ( int i = 1 ; i <= M; i++) {
int numerator = N * i - N + M - i;
int denominator = M - 1 ;
if (numerator % denominator == 0 ) {
int j = numerator / denominator;
if (j >= 1 && j <= N)
count++;
}
}
}
else {
for ( int j = 1 ; j <= N; j++) {
int numerator = M * j - M + N - j;
int denominator = N - 1 ;
if (numerator % denominator == 0 ) {
int i = numerator / denominator;
if (i >= 1 && i <= M)
count++;
}
}
}
return count;
}
public static void main (String[] args)
{
int M = 3 , N = 5 ;
System.out.println( getCount(M, N));
}
}
|
Python3
def getCount(M, N):
count = 0 ;
if (M = = 1 ):
return N;
if (N = = 1 ):
return M;
if (N > M):
for i in range ( 1 , M + 1 ):
numerator = N * i - N + M - i;
denominator = M - 1 ;
if (numerator % denominator = = 0 ):
j = numerator / denominator;
if (j > = 1 and j < = N):
count + = 1 ;
else :
for j in range ( 1 , N + 1 ):
numerator = M * j - M + N - j;
denominator = N - 1 ;
if (numerator % denominator = = 0 ):
i = numerator / denominator;
if (i > = 1 and i < = M):
count + = 1 ;
return count;
if __name__ = = '__main__' :
M, N = 3 , 5 ;
print (getCount(M, N));
|
C#
using System;
class GFG {
static int getCount( int M, int N)
{
int count = 0;
if (M == 1)
return N;
if (N == 1)
return M;
if (N > M) {
for ( int i = 1; i <= M; i++) {
int numerator = N * i - N + M - i;
int denominator = M - 1;
if (numerator % denominator == 0) {
int j = numerator / denominator;
if (j >= 1 && j <= N)
count++;
}
}
}
else {
for ( int j = 1; j <= N; j++) {
int numerator = M * j - M + N - j;
int denominator = N - 1;
if (numerator % denominator == 0) {
int i = numerator / denominator;
if (i >= 1 && i <= M)
count++;
}
}
}
return count;
}
public static void Main ()
{
int M = 3, N = 5;
Console.WriteLine( getCount(M, N));
}
}
|
PHP
<?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 ) ;
?>
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(M)
Auxiliary Space: O(1)
Complexity can be reduced to O(min(M, N)) by establishing relation of i in terms of j(Eq. 2) and iterating for all possible j in case N<M and by establishing relation j in terms of i(Eq. 1) and iterating for all possible i otherwise.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...