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:
Explanation: 
Possible subarrays are – 
{{1}, {6, 10, 9, 2, 1}, {9, 2, 1, 113}}
Input: arr[] = {1} 
Output:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementationn to count
// subarrays having sum
// as  a perfect cube
 
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
// Function to check for
// perfect cube or not
bool isCubicSquare(int x)
{
    int curoot = round(pow(x, 1.0 / 3.0));
 
    if (curoot * curoot * curoot == x)
        return true;
    return false;
}
 
// Function to count the subarray
// whose sum is a perfect cube
int count(int arr[], int n)
{
    int pre[n + 1];
 
    pre[0] = 0;
 
    // Loop to find the prefix sum
    // of the array
    for (int i = 1; i <= n; i++) {
        pre[i] = pre[i - 1] + arr[i - 1];
    }
 
    int ans = 0;
 
    // Loop to take every
    // possible subarrays
    for (int i = 0; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
             
            // check for every
            // possible subarrays
            if (isCubicSquare((double)
                   (pre[j] - pre[i]))) {
                ans++;
            }
        }
    }
 
    return ans;
}
 
// Driver Code
int32_t main()
{
    int arr[6] = { 6, 10, 9, 2, 1, 113 };
 
    cout << count(arr, 6);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementationn to count subarrays 
// having sum as a perfect cube
import java.lang.Math;
class GFG{
     
// Function to check for
// perfect cube or not
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;
}
     
// Function to count the subarray
// whose sum is a perfect cube
public static int count(int arr[], int n)
{
    int[] pre = new int[n + 1];
    pre[0] = 0;
     
    // Loop to find the prefix sum
    // of the array
    for(int i = 1; i <= n; i++)
    {
       pre[i] = pre[i - 1] + arr[i - 1];
    }
     
    int ans = 0;
     
    // Loop to take every
    // possible subarrays
    for(int i = 0; i <= n; i++)
    {
       for(int j = i + 1; j <= n; j++)
       {
            
          // Check for every
          // possible subarrays
          if (isCubicSquare((pre[j] - pre[i])))
          {
              ans++;
          }
       }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 6, 10, 9, 2, 1, 113 };
 
    System.out.print(count(arr, 6));
}
}
 
// This code is contributed by divyeshrabadiya07

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementationn to count
# subarrays having sum
# as  a perfect cube
 
# Function to check for
# perfect cube or not
def isCubicSquare(x):
   
    curoot = round(pow(x,
                   1.0 / 3.0))
 
    if (curoot * curoot *
        curoot == x):
        return True
    return False
 
# Function to count the subarray
# whose sum is a perfect cube
def count(arr, n):
 
    pre = [0] * (n + 1)
    pre[0] = 0
 
    # Loop to find the prefix
    # sum of the array
    for  i in range (1, n + 1):
        pre[i] = pre[i - 1] + arr[i - 1]
    
    ans = 0
 
    # Loop to take every
    # possible subarrays
    for i in range (n + 1):
        for j in range (i + 1, n + 1):
             
            # Check for every
            # possible subarrays
            if (isCubicSquare((pre[j] -
                               pre[i]))):
                ans += 1
      
    return ans
 
# Driver Code
if __name__ == "__main__":
   
    arr = [6, 10, 9, 2, 1, 113]
    print (count(arr, 6))
 
# This code is contributed by Chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementationn to count subarrays
// having sum as a perfect cube
using System;
class GFG{
     
// Function to check for
// perfect cube or not
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;
}
     
// Function to count the subarray
// whose sum is a perfect cube
public static int count(int []arr, int n)
{
    int[] pre = new int[n + 1];
    pre[0] = 0;
     
    // Loop to find the prefix sum
    // of the array
    for(int i = 1; i <= n; i++)
    {
       pre[i] = pre[i - 1] + arr[i - 1];
    }
     
    int ans = 0;
     
    // Loop to take every
    // possible subarrays
    for(int i = 0; i <= n; i++)
    {
       for(int j = i + 1; j <= n; j++)
       {
            
          // Check for every
          // possible subarrays
          if (isCubicSquare((pre[j] - pre[i])))
          {
              ans++;
          }
       }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int []arr = { 6, 10, 9, 2, 1, 113 };
 
    Console.Write(count(arr, 6));
}
}
 
// This code is contributed by Code_Mech

chevron_right


Output: 

3



 

Performance Analysis:  

  • Time Complexity: O(N2)
  • Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.