给定一个整数N ,任务是检查N 是否可以表示为两个正完美立方体的和。
例子:
Input: N = 28
Output: Yes
Explanation:
Since, 28 = 27 + 1 = 33 + 13.
Therefore, the required answer is Yes.
Input: N = 34
Output: No
方法:这个想法是将所有数字从1到N 的立方根的完美立方体存储在 Map 中,并检查N 是否可以表示为 Map 中存在的两个数字的总和。请按照以下步骤解决问题:
- 初始化一个有序映射,比如立方体,以按排序顺序存储前 N 个自然数的完美立方体。
- 遍历地图并检查总和等于N 的对。
- 如果发现这样的一对具有总和N ,则打印“是” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N can be represented
// as sum of two perfect cubes or not
void sumOfTwoPerfectCubes(int N)
{
// Stores the perfect cubes
// of first N natural numbers
map cubes;
for (int i = 1; i * i * i <= N; i++)
cubes[i * i * i] = i;
// Traverse the map
map::iterator itr;
for (itr = cubes.begin();
itr != cubes.end(); itr++) {
// Stores first number
int firstNumber = itr->first;
// Stores second number
int secondNumber = N - itr->first;
// Search the pair for the first
// number to obtain sum N from the Map
if (cubes.find(secondNumber)
!= cubes.end()) {
cout << "True";
return;
}
}
// If N cannot be represented as
// sum of two positive perfect cubes
cout << "False";
}
// Driver Code
int main()
{
int N = 28;
// Function call to check if N
// can be represented as
// sum of two perfect cubes or not
sumOfTwoPerfectCubes(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if N can be represented
// as sum of two perfect cubes or not
public static void sumOfTwoPerfectCubes(int N)
{
// Stores the perfect cubes
// of first N natural numbers
HashMap cubes = new HashMap<>();
for (int i = 1; i * i * i <= N; i++)
cubes.put((i * i * i), i);
// Traverse the map
Iterator > itr
= cubes.entrySet().iterator();
while (itr.hasNext())
{
Map.Entry entry = itr.next();
// Stores first number
int firstNumber = entry.getKey();
// Stores second number
int secondNumber = N - entry.getKey();
// Search the pair for the first
// number to obtain sum N from the Map
if (cubes.containsKey(secondNumber))
{
System.out.println("True");
return;
}
}
// If N cannot be represented as
// sum of two positive perfect cubes
System.out.println("False");
}
// Driver code
public static void main(String[] args)
{
int N = 28;
// Function call to check if N
// can be represented as
// sum of two perfect cubes or not
sumOfTwoPerfectCubes(N);
}
}
// This code is contributed by shailjapriya.
Python3
# Python3 program for the above approach
# Function to check if N can be represented
# as sum of two perfect cubes or not
def sumOfTwoPerfectCubes(N) :
# Stores the perfect cubes
# of first N natural numbers
cubes = {}
i = 1
while i*i*i <= N :
cubes[i*i*i] = i
i += 1
# Traverse the map
for itr in cubes :
# Stores first number
firstNumber = itr
# Stores second number
secondNumber = N - itr
# Search the pair for the first
# number to obtain sum N from the Map
if secondNumber in cubes :
print("True", end = "")
return
# If N cannot be represented as
# sum of two positive perfect cubes
print("False", end = "")
N = 28
# Function call to check if N
# can be represented as
# sum of two perfect cubes or not
sumOfTwoPerfectCubes(N)
# This code is contributed by divyeshrabadiya07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
// Function to check if N can be represented
// as sum of two perfect cubes or not
public static void sumOfTwoPerfectCubes(int N)
{
// Stores the perfect cubes
// of first N natural numbers
Dictionary cubes = new Dictionary();
for (int i = 1; i * i * i <= N; i++)
cubes.Add((i * i * i), i);
var val = cubes.Keys.ToList();
foreach(var key in val)
{
// Stores first number
int firstNumber = cubes[1];
// Stores second number
int secondNumber = N - cubes[1];
// Search the pair for the first
// number to obtain sum N from the Map
if (cubes.ContainsKey(secondNumber))
{
Console.Write("True");
return;
}
}
// If N cannot be represented as
// sum of two positive perfect cubes
Console.Write("False");
}
// Driver Code
static public void Main()
{
int N = 28;
// Function call to check if N
// can be represented as
// sum of two perfect cubes or not
sumOfTwoPerfectCubes(N);
}
}
// This code is contributed by code_hunt.
Javascript
输出
True
时间复杂度: O(N 1/3 * log(N 1/3 ))
辅助空间: O(N 1/3 )