📜  数组中总和为完美立方体的对数

📅  最后修改于: 2021-09-06 06:43:25             🧑  作者: Mango

给定大小为N的不同元素的数组arr ,任务是找到数组中总和为完美立方体的对的总数。
例子:

天真的方法:使用嵌套循环并检查每个可能的对,以确定它们的总和是否是完美的立方体。当数组的长度非常大时,此技术无效。
有效的方法:

  • 将数组的所有元素存储在 HashSet 中,并将最大两个元素的总和保存在名为max的变量中。
  • 很明显,数组中任意两个元素的总和不会超过max 。因此,找到所有最大的完美立方体并将其保存在名为Perfectcubes的 ArrayList 中。
  • 现在对于数组中的每个元素说arr[i]和保存在perfectcubes 中的每个完美立方体,检查Perfectcubes.get(i) – arr[i] 是否存在于nums 中,即原始数组中是否有任何元素当与当前选择的元素一起添加时,可以从列表中获得任何完美的立方体。
  • 如果满足上述条件,则增加计数变量。
  • 最后打印count的值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
#include
using namespace std;
 
// Function to return an ArrayList containing
// all the perfect cubes upto n
vector getPerfectcubes(int n)
{
 
    vectorperfectcubes;
    int current = 1;
    int i = 1;
 
    // while current perfect cube is
    // less than or equal to n
    while (current <= n)
    {
        perfectcubes.push_back(current);
        i += 1;
        current = int(pow(i, 3));
    }
    return perfectcubes;
}
 
// Function to print the sum of maximum
// two elements from the array
int maxPairSum(int arr[],int n)
{
 
    int max = 0;
    int secondMax = 0;
    if (arr[0] > arr[1])
    {
        max = arr[0];
        secondMax = arr[1];
    }
    else
    {
        max = arr[1];
        secondMax = arr[0];
    }
    for (int i = 2; i < n; i++)
    {
        if (arr[i] > max)
        {
            secondMax = max;
            max = arr[i];
        }
        else if (arr[i] > secondMax)
            secondMax = arr[i];
    }
    return (max + secondMax);
}
 
// Function to return the count of numbers that
// when added with n give a perfect cube
int countPairsWith(int n, vector perfectcubes, vector nums)
{
 
    int count = 0;
    int len=perfectcubes.size();
    for (int i = 0; i < len; i++)
    {
        int temp = perfectcubes[i] - n;
 
        // temp > n is checked so that pairs
        // (x, y) and (y, x) don't get counted twice
        if (temp > n)
        {
            for(auto j=nums.begin();j!=nums.end();j++)
            {
                if((*j)==temp)
                    count += 1;
            }
        }
    }
    return count;
}
 
// Function to count the pairs whose
// sum is a perfect cube
int countPairs(int arr[],int n)
{
 
    // Sum of the maximum two elements
    // from the array
    int max = maxPairSum(arr,n);
 
    // List of perfect cubes upto max
    vectorperfectcubes = getPerfectcubes(max);
 
    // Contains all the array elements
    vectornums;
    for (int i = 0 ; i < n; i++)
        nums.push_back(arr[i]);
 
    int count = 0;
    for (int i = 0; i < n; i++)
    {
 
        // Add count of the elements that when
        // added with arr[i] give a perfect cube
        count += countPairsWith(arr[i], perfectcubes, nums);
    }
    return count;
 
}
 
// Driver code
int main()
{
    int arr[] = { 2, 6, 18, 9, 999, 1 };
    int n=sizeof(arr)/sizeof(arr[0]);
    cout<<(countPairs(arr,n));
     
}
 
// This code is contributed by chitranayal


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
    // Function to return an ArrayList containing
    // all the perfect cubes upto n
    static List getPerfectcubes(int n) {
 
        List perfectcubes = new ArrayList();
        int current = 1;
        int i = 1;
 
        // while current perfect cube is
        // less than or equal to n
        while (current <= n) {
            perfectcubes.add(current);
            i += 1;
            current = (int) (Math.pow(i, 3));
        }
        return perfectcubes;
    }
 
    // Function to print the sum of maximum
    // two elements from the array
    static int maxPairSum(int[] arr) {
 
        int n = arr.length;
        int max = 0;
        int secondMax = 0;
        if (arr[0] > arr[1]) {
            max = arr[0];
            secondMax = arr[1];
        } else {
            max = arr[1];
            secondMax = arr[0];
        }
        for (int i = 2; i < n; i++) {
            if (arr[i] > max) {
                secondMax = max;
                max = arr[i];
            } else if (arr[i] > secondMax) {
                secondMax = arr[i];
            }
        }
        return (max + secondMax);
    }
 
    // Function to return the count of numbers that
    // when added with n give a perfect cube
    static int countPairsWith(int n, List
            perfectcubes, List nums) {
 
        int count = 0;
        for (int i = 0; i < perfectcubes.size(); i++) {
            int temp = perfectcubes.get(i) - n;
 
            // temp > n is checked so that pairs
            // (x, y) and (y, x) don't get counted twice
            if (temp > n && (nums.contains(temp)))
                count += 1;
        }
        return count;
    }
 
    // Function to count the pairs whose
    // sum is a perfect cube
    static int countPairs(int[] arr) {
 
        int n = arr.length;
 
        // Sum of the maximum two elements
        // from the array
        int max = maxPairSum(arr);
 
        // List of perfect cubes upto max
        List perfectcubes = getPerfectcubes(max);
 
        // Contains all the array elements
        List nums = new ArrayList();
        for (int i = 0; i < n; i++) {
            nums.add(arr[i]);
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
 
            // Add count of the elements that when
            // added with arr[i] give a perfect cube
            count += countPairsWith(arr[i], perfectcubes, nums);
        }
        return count;
    }
 
    // Driver code
    public static void main(String[] agrs) {
        int[] arr = { 2, 6, 18, 9, 999, 1 };
        System.out.print(countPairs(arr));
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return an ArrayList containing
# all the perfect cubes upto n
def getPerfectcubes(n):
 
    perfectcubes = [];
    current = 1;
    i = 1;
 
    # while current perfect cube is
    # less than or equal to n
    while (current <= n):
        perfectcubes.append(current);
        i += 1;
        current = int(pow(i, 3));
 
    return perfectcubes;
 
# Function to print the sum of maximum
# two elements from the array
def maxPairSum(arr):
 
    n = len(arr);
    max = 0;
    secondMax = 0;
    if (arr[0] > arr[1]):
        max = arr[0];
        secondMax = arr[1];
    else:
        max = arr[1];
        secondMax = arr[0];
 
    for i in range(2, n):
        if (arr[i] > max):
            secondMax = max;
            max = arr[i];
        elif (arr[i] > secondMax):
            secondMax = arr[i];
 
    return (max + secondMax);
 
# Function to return the count of numbers that
# when added with n give a perfect cube
def countPairsWith(n, perfectcubes, nums):
 
    count = 0;
    for i in range(len(perfectcubes)):
        temp = perfectcubes[i] - n;
 
        # temp > n is checked so that pairs
        # (x, y) and (y, x) don't get counted twice
        if (temp > n and (temp in nums)):
            count += 1;
 
    return count;
 
# Function to count the pairs whose
# sum is a perfect cube
def countPairs(arr):
 
    n = len(arr);
 
    # Sum of the maximum two elements
    # from the array
    max = maxPairSum(arr);
 
    # List of perfect cubes upto max
    perfectcubes = getPerfectcubes(max);
 
    # Contains all the array elements
    nums = [];
    for i in range(n):
        nums.append(arr[i]);
 
    count = 0;
    for i in range(n):
 
        # Add count of the elements that when
        # added with arr[i] give a perfect cube
        count += countPairsWith(arr[i],
                perfectcubes, nums);
    return count;
 
# Driver code
arr = [ 2, 6, 18, 9, 999, 1 ];
print(countPairs(arr));


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
  
    // Function to return an List containing
    // all the perfect cubes upto n
    static List getPerfectcubes(int n) {
  
        List perfectcubes = new List();
        int current = 1;
        int i = 1;
  
        // while current perfect cube is
        // less than or equal to n
        while (current <= n) {
            perfectcubes.Add(current);
            i += 1;
            current = (int) (Math.Pow(i, 3));
        }
        return perfectcubes;
    }
  
    // Function to print the sum of maximum
    // two elements from the array
    static int maxPairSum(int[] arr) {
  
        int n = arr.Length;
        int max = 0;
        int secondMax = 0;
        if (arr[0] > arr[1]) {
            max = arr[0];
            secondMax = arr[1];
        } else {
            max = arr[1];
            secondMax = arr[0];
        }
        for (int i = 2; i < n; i++) {
            if (arr[i] > max) {
                secondMax = max;
                max = arr[i];
            } else if (arr[i] > secondMax) {
                secondMax = arr[i];
            }
        }
        return (max + secondMax);
    }
  
    // Function to return the count of numbers that
    // when added with n give a perfect cube
    static int countPairsWith(int n, List
            perfectcubes, List nums) {
  
        int count = 0;
        for (int i = 0; i < perfectcubes.Count; i++) {
            int temp = perfectcubes[i] - n;
  
            // temp > n is checked so that pairs
            // (x, y) and (y, x) don't get counted twice
            if (temp > n && (nums.Contains(temp)))
                count += 1;
        }
        return count;
    }
  
    // Function to count the pairs whose
    // sum is a perfect cube
    static int countPairs(int[] arr) {
  
        int n = arr.Length;
  
        // Sum of the maximum two elements
        // from the array
        int max = maxPairSum(arr);
  
        // List of perfect cubes upto max
        List perfectcubes = getPerfectcubes(max);
  
        // Contains all the array elements
        List nums = new List();
        for (int i = 0; i < n; i++) {
            nums.Add(arr[i]);
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
  
            // Add count of the elements that when
            // added with arr[i] give a perfect cube
            count += countPairsWith(arr[i], perfectcubes, nums);
        }
        return count;
    }
  
    // Driver code
    public static void Main(String[] agrs) {
        int[] arr = { 2, 6, 18, 9, 999, 1 };
        Console.Write(countPairs(arr));
    }
}
 
// This code contributed by Rajput-Ji


Javascript


输出:
3

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