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
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++
// 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; } |
Java
// 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. |
Python3
# Python3 Program to count the number # of positions with same address # in row major and column major order # Returns count of # required positions def 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 in range ( 1 , M + 1 ): 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 + = 1 ; else : # iterating for all possible j for j in range ( 1 , N + 1 ): 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 + = 1 ; return count; # Driver Code if __name__ = = '__main__' : M, N = 3 , 5 ; print (getCount(M, N)); # This code is contributed by Rajput-Ji |
C#
// 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. |
PHP
<?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. ?> |
Javascript
<script> // JavaScript 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) { let 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 (let i = 1; i <= M; i++) { let numerator = N * i - N + M - i; let denominator = M - 1; // checking if j is integer if (numerator % denominator == 0) { let j = parseInt(numerator / denominator, 10); // checking if j lies b/w 1 to N if (j >= 1 && j <= N) count++; } } } else { // iterating for all possible j for (let j = 1; j <= N; j++) { let numerator = M * j - M + N - j; let denominator = N - 1; // checking if i is integer if (numerator % denominator == 0) { let i = parseInt(numerator / denominator, 10); // checking if i lies b/w 1 to M if (i >= 1 && i <= M) count++; } } } return count; } let M = 3, N = 5; document.write( getCount(M, N)); </script> |
Output
3
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.
Please Login to comment...