给定一个由N个正整数组成的数组arr [] ,任务是计算仅由质子数组成的子数组的数量。
例子:
Input: arr[] = {5, 6, 12, 3, 4}
Output: 3
Explanation: The subarray that consists only of pronic numbers are:
- {6}
- {12}
- {6, 12}
Therefore, the total count of such subarrays is 3.
Input: arr[] = {0, 4, 20, 30, 5}
Output: 4
天真的方法:解决问题的最简单方法是生成给定数组的所有可能的子数组,并对仅由质子数组成的那些子数组进行计数。检查完所有子数组后,打印获得的计数。
时间复杂度: O(√M* N 3 ),其中M是数组中存在的最大元素
辅助空间: O(N)
高效方法:可以通过跟踪质子数的连续序列,然后计算形成的子阵列的数量来优化上述方法。
请按照以下步骤解决问题:
- 初始化一个变量,例如count ,以存储子数组的总数,并初始化一个变量C ,以存储连续数组元素的数量,这些连续数组元素是质子数。
- 遍历给定数组arr []并执行以下步骤:
- 如果当前元素arr [i]是一个质子数,则将C增加1 。
- 否则,以C *(C – 1)/ 2递增计数,以计算具有质子数的C元素的子数组的数目,并将C更新为0 。
- 将count的值递增为C *(C – 1)/ 2 。
- 完成上述步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the approach
#include
#include
using namespace std;
// Function to check if a number
// is pronic number or not
bool isPronic(int n)
{
// Iterate over the range [1, sqrt(N)]
int range = sqrt(n);
for(int i = 0; i < range + 1; i++)
{
// Return true if N is pronic
if (i * (i + 1) == n)
return true;
}
// Otherwise, return false
return false;
}
// Function to count the number of
// subarrays consisting of pronic numbers
int countSub(int *arr, int n)
{
// Stores the count of subarrays
int ans = 0;
// Stores the number of consecutive
// array elements which are pronic
int ispro = 0;
// Traverse the aray
for(int i = 0; i < n; i++)
{
// If i is pronic
if (isPronic(arr[i]))
ispro += 1;
else
ispro = 0;
ans += ispro;
}
// Return the total count
return ans;
}
// Driver code
int main()
{
int arr[5] = {5, 6, 12, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
cout << countSub(arr, n);
return 0;
}
// This code is contributed by rohitsingh07052
Java
// Java program for the approach
import java.lang.*;
class GFG
{
// Function to check if a number
// is pronic number or not
static boolean isPronic(int n)
{
// Iterate over the range [1, sqrt(N)]
int range = (int)Math.sqrt(n);
for(int i = 0; i < range + 1; i++)
{
// Return true if N is pronic
if (i * (i + 1) == n)
return true;
}
// Otherwise, return false
return false;
}
// Function to count the number of
// subarrays consisting of pronic numbers
static int countSub(int[] arr, int n)
{
// Stores the count of subarrays
int ans = 0;
// Stores the number of consecutive
// array elements which are pronic
int ispro = 0;
// Traverse the aray
for(int i = 0; i < n; i++)
{
// If i is pronic
if (isPronic(arr[i]))
ispro += 1;
else
ispro = 0;
ans += ispro;
}
// Return the total count
return ans;
}
// Driver code
public static void main(String[] args)
{
int[] arr = {5, 6, 12, 3, 4};
int n = arr.length;
System.out.print(countSub(arr, n));
}
}
// This code is contributed by shivani
Python3
# Python3 program for the approach
# Function to check if a number
# is pronic number or not
def isPronic(n):
# Iterate over the range [1, sqrt(N)]
for i in range(int(n ** (1 / 2)) + 1):
# Return true if N is pronic
if i * (i + 1) == n:
return True
# Otherwise, return false
return False
# Function to count the number of
# subarrays consisting of pronic numbers
def countSub(arr):
# Stores the count of subarrays
ans = 0
# Stores the number of consecutive
# array elements which are pronic
ispro = 0
# Traverse the aray
for i in arr:
# If i is pronic
if isPronic(i):
ispro += 1
else:
ispro = 0
ans += ispro
# Return the total count
return ans
# Driver Code
arr = [5, 6, 12, 3, 4]
print(countSub(arr))
C#
// C# program for the approach
using System;
class GFG
{
// Function to check if a number
// is pronic number or not
static bool isPronic(int n)
{
// Iterate over the range [1, sqrt(N)]
int range = (int)Math.Sqrt(n);
for(int i = 0; i < range + 1; i++)
{
// Return true if N is pronic
if (i * (i + 1) == n)
return true;
}
// Otherwise, return false
return false;
}
// Function to count the number of
// subarrays consisting of pronic numbers
static int countSub(int[] arr, int n)
{
// Stores the count of subarrays
int ans = 0;
// Stores the number of consecutive
// array elements which are pronic
int ispro = 0;
// Traverse the aray
for(int i = 0; i < n; i++)
{
// If i is pronic
if (isPronic(arr[i]))
ispro += 1;
else
ispro = 0;
ans += ispro;
}
// Return the total count
return ans;
}
// Driver code
static void Main() {
int[] arr = {5, 6, 12, 3, 4};
int n = arr.Length;
Console.WriteLine(countSub(arr, n));
}
}
// This code is contributed by divyesh072019.
Javascript
输出
3
时间复杂度: O(N * sqrt(M)),其中M是数组中存在的最大元素。
辅助空间: O(1)