在大小为 n 的矩阵中计算 k 的频率,其中 matrix(i, j) = i+j
给定一个大小为 n*n 的矩阵。计算该矩阵中给定元素 k 的频率。这里的基础索引是 1。
例子:
Input : n = 4, k = 7
Output : 2
Explanation
The matrix will be
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
in the given matrix where M(i, j) = i+j,
frequency of 7 is 2
Input : n = 5, k = 4
Output : 3
Explanation
The matrix will be
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
Explanation
In the given matrix where M(i, j) = i+j,
frequency of 4 is 3
第一种方法
1) 构造一个大小为 n*n 的矩阵。
2) 用 M(i, j)=i+j 填充值。(记得这里的基索引是 1)
3)迭代遍历矩阵并计算给定元素的频率。
这种方法效率不高,因为如果矩阵大小非常大,则会导致超出时间限制。时间复杂度为 O(n^2)。
高效的方法
在这种方法中,我们避免创建大小为 n*n 的矩阵。
例如
如果 n = 10,矩阵将是
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
现在,请注意辅助对角线中的值是如何相同的,我们还可以在计数中找到一个模式,它像 1、2、3、4 一样增加,
在这里我们可以看到
如果 (n+1)>=k 则 k 的频率为 k-1
其他频率将为 2*n+1-k
CPP
// CPP program to find the frequency of k
// in matrix where m(i, j)=i+j
#include
using namespace std;
int find(int n, int k)
{
if (n + 1 >= k)
return (k - 1);
else
return (2 * n + 1 - k);
}
// Driver Code
int main()
{
int n = 4, k = 7;
int freq = find(n, k);
if (freq < 0)
cout << " element not exist \n ";
else
cout << " Frequency of " << k
<< " is " << freq << "\n";
return 0;
}
Java
// Java program to find the
// frequency of k in matrix
// in matrix where m(i, j)=i+j
import java.util.*;
import java.lang.*;
public class GfG{
public static int find(int n, int k)
{
if (n + 1 >= k)
return (k - 1);
else
return (2 * n + 1 - k);
}
// Driver function
public static void main(String argc[])
{
int n = 4, k = 7;
int freq = find(n, k);
if (freq < 0)
System.out.print(" element"
+ " not exist \n ");
else
System.out.print(" Frequency"
+ " of " + k + " is " +
freq + "\n");
}
}
// This code is contributed by Sagar Shukla
Python3
# Python program to find
# the frequency of k
# in matrix where
# m(i, j)=i+j
import math
def find( n, k):
if (n + 1 >= k):
return (k - 1)
else:
return (2 * n + 1 - k)
# Driver Code
n = 4
k = 7
freq = find(n, k)
if (freq < 0):
print ( " element not exist")
else:
print(" Frequency of " , k ," is " , freq )
# This code is contributed
# by Gitanjali.
C#
// C# program to find the
// frequency of k in matrix
// in matrix where m(i, j)=i+j
using System;
public class GfG{
public static int find(int n, int k)
{
if (n + 1 >= k)
return (k - 1);
else
return (2 * n + 1 - k);
}
// Driver function
public static void Main()
{
int n = 4, k = 7;
int freq = find(n, k);
if (freq < 0)
Console.WriteLine(" element"
+ " not exist ");
else
Console.WriteLine(" Frequency"
+ " of " + k + " is " +
freq );
}
}
// This code is contributed by vt_m
PHP
= $k)
return ($k - 1);
else
return (2 * $n + 1 - $k);
}
// Driver Code
$n = 4;
$k = 7;
$freq = find($n, $k);
if ($freq < 0)
echo " element not exist \n ";
else
echo " Frequency of " , $k
, " is " , $freq , "\n";
// This code is contributed by anuj_67.
?>
Javascript
输出:
Frequency of 7 is 2