给定两个正整数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个 行包含范围[1,N]中i的所有倍数。因此, X出现在第i个 当且仅当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.
输出:
4
时间复杂度: O(N)
辅助空间: O(1)