Related Articles
Number of subarrays consisting only of Pronic Numbers
• Last Updated : 16 Apr, 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:

1. {6}
2. {12}
3. {6, 12}

Therefore, the total count of such subarrays is 3.

Input: arr[] = {0, 4, 20, 30, 5}
Output:

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++

 `// 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, 6, 12, 3, 4};``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ` `    ``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

 ``
Output
`3`

Time Complexity: O(N*sqrt(M)), where M is the maximum element present in the array.
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up