Number of positions with Same address in row major and column major order

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

Lets 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to count the number
// of positions with same address
// in row major and column major order
#include <bits/stdc++.h>
  
using namespace std;
  
// Returns count of required positions
int getCount(int M, int N)
{
    int count = 0;
  
    // horizontal 1D array
    if (M == 1)
        return N;
  
    // vertical 1D array
    if (N == 1)
        return M;
  
    if (N > M) {
  
        // iterating for all possible i
        for (int i = 1; i <= M; i++) {
            int numerator = N * i - N + M - i;
            int denominator = M - 1;
  
            // checking if j is integer
            if (numerator % denominator == 0) {
                int j = numerator / denominator;
  
                // checking if j lies b/w 1 to N
                if (j >= 1 && j <= N)
                    count++;
            }
        }
    }
    else {
  
        // iterating for all possible j
        for (int j = 1; j <= N; j++) {
            int numerator = M * j - M + N - j;
            int denominator = N - 1;
  
            // checking if i is integer
            if (numerator % denominator == 0) {
                int i = numerator / denominator;
  
                // checking if i lies b/w 1 to M
                if (i >= 1 && i <= M)
                    count++;
            }
        }
    }
    return count;
}
  
// Driver Code
int main()
{
    int M = 3, N = 5;
    cout << getCount(M, N) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to count the number
// of positions with same address
// in row major and column major order
import java.io.*;
class GFG {
  
// Returns count of
// required positions
static int getCount(int M, int N)
{
    int count = 0;
  
    // horizontal 1D array
    if (M == 1)
        return N;
  
    // vertical 1D array
    if (N == 1)
        return M;
  
    if (N > M) {
  
        // iterating for all possible i
        for (int i = 1; i <= M; i++) {
            int numerator = N * i - N + M - i;
            int denominator = M - 1;
  
            // checking if j is integer
            if (numerator % denominator == 0) {
                int j = numerator / denominator;
  
                // checking if j lies b/w 1 to N
                if (j >= 1 && j <= N)
                    count++;
            }
        }
    }
    else {
  
        // iterating for all possible j
        for (int j = 1; j <= N; j++) {
            int numerator = M * j - M + N - j;
            int denominator = N - 1;
  
            // checking if i is integer
            if (numerator % denominator == 0) {
                int i = numerator / denominator;
  
                // checking if i lies b/w 1 to M
                if (i >= 1 && i <= M)
                    count++;
            }
        }
    }
    return count;
}
  
    // Driver Code
    public static void main (String[] args)
    {
        int M = 3, N = 5;
        System.out.println( getCount(M, N));
    }
}
  
// This code is contributed by vt_m.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to count the number
// of positions with same address
// in row major and column major order
using System;
class GFG {
  
    // Returns count of
    // required positions
    static int getCount(int M, int N)
    {
        int count = 0;
      
        // horizontal 1D array
        if (M == 1)
            return N;
      
        // vertical 1D array
        if (N == 1)
            return M;
      
        if (N > M) {
      
            // iterating for all possible i
            for (int i = 1; i <= M; i++) {
                int numerator = N * i - N + M - i;
                int denominator = M - 1;
      
                // checking if j is integer
                if (numerator % denominator == 0) {
                    int j = numerator / denominator;
      
                    // checking if j lies b/w 1 to N
                    if (j >= 1 && j <= N)
                        count++;
                }
            }
        }
        else {
      
            // iterating for all possible j
            for (int j = 1; j <= N; j++) {
                int numerator = M * j - M + N - j;
                int denominator = N - 1;
      
                // checking if i is integer
                if (numerator % denominator == 0) {
                    int i = numerator / denominator;
      
                    // checking if i lies b/w 1 to M
                    if (i >= 1 && i <= M)
                        count++;
                }
            }
        }
        return count;
    }
  
    // Driver Code
    public static void Main ()
    {
        int M = 3, N = 5;
        Console.WriteLine( getCount(M, N));
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to count the number
// of positions with same address
// in row major and column major order
  
// Returns count of required positions
function getCount( $M, $N)
{
    $count = 0;
  
    // horizontal 1D array
    if ($M == 1)
        return $N;
  
    // vertical 1D array
    if ($N == 1)
        return $M;
  
    if ($N > $M)
    {
  
        // iterating for all possible i
        for($i = 1; $i <= $M; $i++)
        {
            $numerator = $N * $i - $N + $M - $i;
            $denominator = $M - 1;
  
            // checking if j is integer
            if ($numerator % $denominator == 0) 
            {
                $j = $numerator / $denominator;
  
                // checking if j lies b/w 1 to N
                if ($j >= 1 and $j <= $N)
                    $count++;
            }
        }
    }
    else 
    {
  
        // iterating for all possible j
        for ( $j = 1; $j <= $N; $j++)
        {
            $numerator = $M * $j - $M + $N - $j;
            $denominator = $N - 1;
  
            // checking if i is integer
            if ($numerator % $denominator == 0)
            {
                $i = $numerator / $denominator;
  
                // checking if i lies b/w 1 to M
                if ($i >= 1 and $i <= $M)
                    $count++;
            }
        }
    }
    return $count;
}
  
    // Driver Code
    $M = 3; $N = 5;
    echo getCount($M, $N) ;
  
// This code is contributed by anuj_67.
?>

chevron_right



Output :

3

Time Complexity: O(M)
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.



My Personal Notes arrow_drop_up

iOS Developer with a keen interest in problem solving

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.