– 给定两个正整数N和X ,任务是计算给定整数X在生成的长度为N 的方阵中的出现次数,矩阵的每个元素都等于其行和列索引的乘积( 1-基于索引)。
例子:
Input: N = 5, X = 6
Output: 2
Explanation:
The 2D array formed is equal to the :
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
There are 2 occurrences of the element X(= 6) in the generated array.
Input: N = 7, X = 12
Output: 4
朴素的方法:参考这篇文章了解最简单的解决问题的方法,通过将行和列索引相乘来构造给定矩阵以获得每个矩阵元素并计算X的出现次数。
时间复杂度: O(N 2 )
辅助空间: O(N 2 )
高效的方法:这个想法是基于观察到第i个 row 包含[1, N]范围内i 的所有倍数。因此, X出现在第i个 row 当且仅当X可被i整除且X / i应小于或等于N 。如果发现为真,则将计数加 1。 按照以下步骤解决问题:
- 初始化一个变量,比如count ,以存储X在生成的矩阵中出现的次数。
- 使用变量i迭代范围[1, N]并执行以下步骤:
- 如果X可被i整除,则将X / i的商存储在一个变量中,例如b 。
- 如果b的值落在[1, N]范围内,则将计数增加1 。
- 完成以上步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the occurrences
// of X in the generated square matrix
int countOccurrences(int n, int x)
{
// Store the required result
int count = 0;
// Iterate over the range [1, N]
for (int i = 1; i <= n; i++) {
// Check if x is a
// multiple of i or not
if (x % i == 0) {
// Check if the other multiple
// exists in the range or not
if (x / i <= n)
count++;
}
}
// Print the result
cout << count;
}
// Driver Code
int main()
{
int N = 7, X = 12;
countOccurrences(N, X);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to count the occurrences
// of X in the generated square matrix
static void countOccurrences(int n, int x)
{
// Store the required result
int count = 0;
// Iterate over the range [1, N]
for (int i = 1; i <= n; i++) {
// Check if x is a
// multiple of i or not
if (x % i == 0) {
// Check if the other multiple
// exists in the range or not
if (x / i <= n)
count++;
}
}
// Print the result
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
int N = 7, X = 12;
countOccurrences(N, X);
}
}
// This code contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to count the occurrences
# of X in the generated square matrix
def countOccurrences(n, x):
# Store the required result
count = 0
# Iterate over the range [1, N]
for i in range(1, n + 1):
# Check if x is a
# multiple of i or not
if (x % i == 0):
# Check if the other multiple
# exists in the range or not
if (x // i <= n):
count += 1
# Print the result
print(count)
# Driver Code
if __name__ == "__main__":
N = 7
X = 12
countOccurrences(N, X)
# This code is contributed by ukasp
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the occurrences
// of X in the generated square matrix
static void countOccurrences(int n, int x)
{
// Store the required result
int count = 0;
// Iterate over the range [1, N]
for (int i = 1; i <= n; i++) {
// Check if x is a
// multiple of i or not
if (x % i == 0) {
// Check if the other multiple
// exists in the range or not
if (x / i <= n)
count++;
}
}
// Print the result
Console.WriteLine(count);
}
// Driver Code
public static void Main(String[] args)
{
int N = 7, X = 12;
countOccurrences(N, X);
}
}
// This code is contributed by splevel62.
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(1)