给定两个正整数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 )
高效方法:为了优化上述方法,该思想基于以下观察结果:矩阵中的每个元素都是2个数的乘积。因此,通过检查X可以表示为2个数字的乘积的方式数量,然后选择位于[1,N]范围内的那些对,可以得出结果。请按照以下步骤解决问题:
- 初始化一个变量,例如count ,以将X的出现次数存储在生成的矩阵中。
- 使用变量i遍历[1,√X]范围,并执行以下步骤:
- 如果i的值除以X,存储在一个变量除以X用i获得的商,说湾
- 如果i和b的值都在[1,N]范围内,请执行以下步骤:
- 检查我是否等于b 。如果确定为真,则表示X是一个完美的正方形,并且行和列将出现一次。因此,将计数增加1 。
- 否则,这意味着它们将发生两次,一次是连续发生,另一次是在一列。因此,将计数增加2 。
- 完成上述步骤后,打印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)
{
// Stores the required result
int count = 0;
//Iterate upto square root of X
for (int i = 1; i < sqrt(X); i++)
{
// Check if i divides X
if (X % i == 0)
{
// Store the quotient obtained
// on dividing X by i
int a = i;
int b = X / i;
// If both the numbers fall in
// the range, update count
if (a <= N && b <= N)
{
if (a == b)
count += 1;
else
count += 2;
}
}
}
// Return the result
return count;
}
// Driver code
int main()
{
// Given N and X
int N = 7;
int X = 12;
// Function Call
cout << countOccurrences(N, X);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to count the occurrences
// of X in the generated square matrix
static int countOccurrences(int N, int X)
{
// Stores the required result
int count = 0;
// Iterate upto square root of X
for (int i = 1; i < Math.sqrt(X); i++) {
// Check if i divides X
if (X % i == 0) {
// Store the quotient obtained
// on dividing X by i
int a = i;
int b = X / i;
// If both the numbers fall in
// the range, update count
if (a <= N && b <= N) {
if (a == b)
count += 1;
else
count += 2;
}
}
}
// Return the result
return count;
}
// Driver code
public static void main(String[] args)
{
// Given N and X
int N = 7;
int X = 12;
// Function Call
System.out.println(countOccurrences(N, X));
}
}
// This code is contributed by Kingsh.
Python3
# Python3 program for the above approach
from math import sqrt
# Function to count the occurrences
# of X in the generated square matrix
def countOccurrences(N, X):
# Stores the required result
count = 0
# Iterate upto square root of X
for i in range(1, int(sqrt(X))+1):
# Check if i divides X
if X % i == 0:
# Store the quotient obtained
# on dividing X by i
a = i
b = X//i
# If both the numbers fall in
# the range, update count
if a <= N and b <= N:
if a == b:
count += 1
else:
count += 2
# Return the result
return count
# Driver Code
if __name__ == '__main__':
# Given N and X
N = 7
X = 12
# Function Call
print(countOccurrences(N, X))
C#
// C# program for above approach
using System;
public class GFG
{
// Function to count the occurrences
// of X in the generated square matrix
static int countOccurrences(int N, int X)
{
// Stores the required result
int count = 0;
// Iterate upto square root of X
for (int i = 1; i < Math.Sqrt(X); i++) {
// Check if i divides X
if (X % i == 0) {
// Store the quotient obtained
// on dividing X by i
int a = i;
int b = X / i;
// If both the numbers fall in
// the range, update count
if (a <= N && b <= N) {
if (a == b)
count += 1;
else
count += 2;
}
}
}
// Return the result
return count;
}
// Driver code
public static void Main(String[] args)
{
// Given N and X
int N = 7;
int X = 12;
// Function Call
Console.Write(countOccurrences(N, X));
}
}
// This code is contributed by code_hunt.
输出:
4
时间复杂度: O(√X)
辅助空间: O(1)