给定大小为M x N的2D数组。计算2D数组中的位置数,其中按行主要顺序的地址等于按列主要顺序的地址。
例子:
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
让我们考虑索引为i,j的元素
Row major address = B + w * (N * (i-1) + j-1)
Column major address = B + w * (M * (j-1) + i-1)
B :数组的基地址
w :数组中每个元素的大小
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)
现在我们已经建立了i和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.
下面是上述方法的实现。
C++
// CPP Program to count the number
// of positions with same address
// in row major and column major order
#include
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
$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.
?>
输出 :
3
时间复杂度:O(M)
通过建立根据j(方程2)的i的关系并在N