给定一个由N 个正整数组成的数组arr[] ,任务是计算仅由 pronic 数组成的子数组的数量。
例子:
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
朴素方法:解决问题的最简单方法是生成给定数组的所有可能子数组,并计算那些仅由 pronic 数组成的子数组。检查所有子数组后,打印获得的计数。
时间复杂度: O(√M * N 3 ),其中M是数组中存在的最大元素
辅助空间: O(N)
有效的方法:上述方法可以通过跟踪连续序列的 pronic 数,然后计算形成的子数组的数量来优化。
请按照以下步骤解决问题:
- 初始化一个变量,比如count来存储子数组的总数,以及一个变量C来存储连续数组元素的计数,这些元素是连续数。
- 遍历给定的数组arr[]并执行以下步骤:
- 如果当前元素arr[i]是pronic number ,则将C增加1 。
- 否则,将count增加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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live