Number of subarrays consisting only of Pronic Numbers
Last Updated :
17 Jun, 2021
Given an array arr[] consisting of N positive integers, the task is to count the number of subarrays consisting only of pronic numbers.
Examples:
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
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays of the given array and count those subarrays made up of pronic numbers only. After checking for all the subarrays, print the count obtained.
Time Complexity: O(?M * N3), where M is the maximum element present in the array
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by keeping the track of continuous sequences of pronic numbers and then, count the number of subarrays formed.
Follow the steps below to solve the problem:
- Initialize a variable, say count, to store the total count of subarrays, and a variable C, to store the count of continuous array elements which are pronic numbers.
- Traverse the given array arr[] and perform the following steps:
- If the current element arr[i] is a pronic number, then increment C by 1.
- Otherwise, increment count by C * (C – 1)/2 to count the number of subarrays with C elements having pronic numbers and update C to 0.
- Increment the value of count as C*(C – 1)/2.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <cmath>
using namespace std;
bool isPronic( int n)
{
int range = sqrt (n);
for ( int i = 0; i < range + 1; i++)
{
if (i * (i + 1) == n)
return true ;
}
return false ;
}
int countSub( int *arr, int n)
{
int ans = 0;
int ispro = 0;
for ( int i = 0; i < n; i++)
{
if (isPronic(arr[i]))
ispro += 1;
else
ispro = 0;
ans += ispro;
}
return ans;
}
int main()
{
int arr[5] = {5, 6, 12, 3, 4};
int n = sizeof (arr) / sizeof (arr[0]);
cout << countSub(arr, n);
return 0;
}
|
Java
import java.lang.*;
class GFG
{
static boolean isPronic( int n)
{
int range = ( int )Math.sqrt(n);
for ( int i = 0 ; i < range + 1 ; i++)
{
if (i * (i + 1 ) == n)
return true ;
}
return false ;
}
static int countSub( int [] arr, int n)
{
int ans = 0 ;
int ispro = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (isPronic(arr[i]))
ispro += 1 ;
else
ispro = 0 ;
ans += ispro;
}
return ans;
}
public static void main(String[] args)
{
int [] arr = { 5 , 6 , 12 , 3 , 4 };
int n = arr.length;
System.out.print(countSub(arr, n));
}
}
|
Python3
def isPronic(n):
for i in range ( int (n * * ( 1 / 2 )) + 1 ):
if i * (i + 1 ) = = n:
return True
return False
def countSub(arr):
ans = 0
ispro = 0
for i in arr:
if isPronic(i):
ispro + = 1
else :
ispro = 0
ans + = ispro
return ans
arr = [ 5 , 6 , 12 , 3 , 4 ]
print (countSub(arr))
|
C#
using System;
class GFG
{
static bool isPronic( int n)
{
int range = ( int )Math.Sqrt(n);
for ( int i = 0; i < range + 1; i++)
{
if (i * (i + 1) == n)
return true ;
}
return false ;
}
static int countSub( int [] arr, int n)
{
int ans = 0;
int ispro = 0;
for ( int i = 0; i < n; i++)
{
if (isPronic(arr[i]))
ispro += 1;
else
ispro = 0;
ans += ispro;
}
return ans;
}
static void Main() {
int [] arr = {5, 6, 12, 3, 4};
int n = arr.Length;
Console.WriteLine(countSub(arr, n));
}
}
|
Javascript
<script>
function isPronic(n)
{
let range = Math.sqrt(n);
for (let i = 0; i < range + 1; i++)
{
if (i * (i + 1) == n)
return true ;
}
return false ;
}
function countSub(arr, n)
{
let ans = 0;
let ispro = 0;
for (let i = 0; i < n; i++)
{
if (isPronic(arr[i]))
ispro += 1;
else
ispro = 0;
ans += ispro;
}
return ans;
}
let arr = [5, 6, 12, 3, 4];
let n = arr.length;
document.write(countSub(arr, n));
</script>
|
Time Complexity: O(N*sqrt(M)), where M is the maximum element present in the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...