📜  总和为完美立方体的子数组的计数

📅  最后修改于: 2021-09-07 02:14:18             🧑  作者: Mango

给定一个数组arr[] ,任务是计算总和为完美立方体的子数组。
例子:

方法:这个想法是找到数组的前缀和,使得任何子数组的和都可以在 O(1) 中计算。然后迭代每个可能的子数组并检查子数组的总和是否为完美立方体,如果是,则将计数增加 1。
下面是上述方法的实现:

C++
// C++ implementationn to count
// subarrays having sum
// as  a perfect cube
 
#include 
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;
}


Java
// 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


Python3
# 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


C#
// 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


Javascript


输出:
3

性能分析:

  • 时间复杂度: O(N 2 )
  • 辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live