Count of subarrays having sum as a perfect cube
Given an array arr[], the task is to count the subarrays having sum as a perfect cube.
Examples:
Input: arr[] = {6, 10, 9, 2, 1, 113}
Output: 3
Explanation:
The subarrays with sum of elements equal to a perfect cube are:
- {1}. Therefore, sum of subarray = 1 (= 1^3).
- {6, 10, 9, 2}. Therefore, sum of subarray = 27 ( = 3^3).
- {9, 2, 1, 113}. Therefore, sum of subarray = 125 ( = 5^3).
Input: arr[] = {1}
Output: 1
Explanation:
There is only one such subarray whose sum is a perfect cube
Approach: The idea is to find the prefix sum of the array such that the sum of any subarray can be computed in O(1). Then iterate over every possible subarray and check that the sum of the subarray is a perfect cube if yes then increment the count by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
bool isCubicSquare( int x)
{
int curoot = round( pow (x, 1.0 / 3.0));
if (curoot * curoot * curoot == x)
return true ;
return false ;
}
int count( int arr[], int n)
{
int pre[n + 1];
pre[0] = 0;
for ( int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + arr[i - 1];
}
int ans = 0;
for ( int i = 0; i <= n; i++) {
for ( int j = i + 1; j <= n; j++) {
if (isCubicSquare(( double )
(pre[j] - pre[i]))) {
ans++;
}
}
}
return ans;
}
int32_t main()
{
int arr[6] = { 6, 10, 9, 2, 1, 113 };
cout << count(arr, 6);
return 0;
}
|
Java
import java.lang.Math;
class GFG{
public static boolean isCubicSquare( int x)
{
double curoot = Math.round(
Math.pow(x, 1.0 / 3.0 ));
if (curoot * curoot * curoot == x)
return true ;
return false ;
}
public static int count( int arr[], int n)
{
int [] pre = new int [n + 1 ];
pre[ 0 ] = 0 ;
for ( int i = 1 ; i <= n; i++)
{
pre[i] = pre[i - 1 ] + arr[i - 1 ];
}
int ans = 0 ;
for ( int i = 0 ; i <= n; i++)
{
for ( int j = i + 1 ; j <= n; j++)
{
if (isCubicSquare((pre[j] - pre[i])))
{
ans++;
}
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 6 , 10 , 9 , 2 , 1 , 113 };
System.out.print(count(arr, 6 ));
}
}
|
Python3
def isCubicSquare(x):
curoot = round ( pow (x,
1.0 / 3.0 ))
if (curoot * curoot *
curoot = = x):
return True
return False
def count(arr, n):
pre = [ 0 ] * (n + 1 )
pre[ 0 ] = 0
for i in range ( 1 , n + 1 ):
pre[i] = pre[i - 1 ] + arr[i - 1 ]
ans = 0
for i in range (n + 1 ):
for j in range (i + 1 , n + 1 ):
if (isCubicSquare((pre[j] -
pre[i]))):
ans + = 1
return ans
if __name__ = = "__main__" :
arr = [ 6 , 10 , 9 , 2 , 1 , 113 ]
print (count(arr, 6 ))
|
C#
using System;
class GFG{
public static bool isCubicSquare( int x)
{
double curoot = Math.Round(
Math.Pow(x, 1.0 / 3.0));
if (curoot * curoot * curoot == x)
return true ;
return false ;
}
public static int count( int []arr, int n)
{
int [] pre = new int [n + 1];
pre[0] = 0;
for ( int i = 1; i <= n; i++)
{
pre[i] = pre[i - 1] + arr[i - 1];
}
int ans = 0;
for ( int i = 0; i <= n; i++)
{
for ( int j = i + 1; j <= n; j++)
{
if (isCubicSquare((pre[j] - pre[i])))
{
ans++;
}
}
}
return ans;
}
public static void Main()
{
int []arr = { 6, 10, 9, 2, 1, 113 };
Console.Write(count(arr, 6));
}
}
|
Javascript
<script>
function isCubicSquare( x)
{
var curoot = Math.round(Math.pow(x, 1.0 / 3.0));
if (curoot * curoot * curoot == x)
return true ;
return false ;
}
function count(arr, n)
{
var pre = Array(n+1);
pre[0] = 0;
for ( var i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + arr[i - 1];
}
var ans = 0;
for ( var i = 0; i <= n; i++) {
for ( var j = i + 1; j <= n; j++) {
if (isCubicSquare(
(pre[j] - pre[i]))) {
ans++;
}
}
}
return ans;
}
var arr = [6, 10, 9, 2, 1, 113 ];
document.write( count(arr, 6));
</script>
|
Performance Analysis:
- Time Complexity: O(N2 log N)
- Auxiliary Space: O(N)
Last Updated :
15 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...