Given an array of n integers, count all different triplets whose sum is equal to the perfect cube i.e, for any i, j, k(i < j < k) satisfying the condition that a[i] + a[j] + a[j] = X3 where X is any integer. 3 ≤ n ≤ 1000, 1 ≤ a[i, j, k] ≤ 5000
Input: N = 5 2 5 1 20 6 Output: 3 Explanation: There are only 3 triplets whose total sum is a perfect cube. Indices Values SUM 0 1 2 2 5 1 8 0 1 3 2 5 20 27 2 3 4 1 20 6 27 Since 8 and 27 are prefect cube of 2 and 3.
Naive approach is to iterate over all the possible numbers by using 3 nested loops and check whether their sum is a perfect cube or not. The approach would be very slow as time complexity can go up to O(n3).
An Efficient approach is to use dynamic programming and basic mathematics. According to the given condition sum of any of three positive integers is not greater than 15000. Therefore there can be only 24(150001/3) cubes are possible in the range of 1 to 15000.
Now instead of iterating all triplets we can do much better by the help of above information. Fixed first two indices i and j such that instead of iterating over all k(j < k ≤ n), we can iterate over all the 24 possible cubes, and for each one, (let's say P) check how many occurrence of P – (a[i] + a[j]) are in a[j+1, j+2, … n].
But if we compute the number of occurrences of a number say K in a[j+1, j+2, … n] then this would again be counted as slow approach and would definitely give TLE. So we have to think of a different approach.
Now here comes to a Dynamic Programming. Since all numbers are smaller than 5000 and n is at most 1000. Hence we can compute a DP array as,
dp[i][K]:= Number of occurrence of K in A[i, i+1, i+2 … n]
Time complexity: O(N2*24)
Auxiliary space: O(107)
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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.
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Construct an Array such that cube sum of all element is a perfect square
- Smallest perfect Cube divisible by all elements of an array
- Perfect cube greater than a given number
- Largest number in an array that is not a perfect cube
- Largest perfect cube number in an Array
- Perfect Cube String
- Check whether N can be a Perfect Cube after adding or subtracting K
- Print N numbers such that their product is a Perfect Cube
- C Program to check whether a number is a Perfect Cube or not
- Program for cube sum of first n natural numbers
- C Program for cube sum of first n natural numbers
- C++ Program for cube sum of first n natural numbers
- Java Program for cube sum of first n natural numbers
- Python Program for cube sum of first n natural numbers
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Find Cube Pairs | Set 1 (A n^(2/3) Solution)
- Find Cube Pairs | Set 2 (A n^(1/3) Solution)
- Centered cube number