- Related Questions & Answers
- Count of pairs in an array whose sum is a perfect square in C++
- Count numbers upto N which are both perfect square and perfect cube in C++
- Maximum Primes whose sum is equal to given N in C++
- Count all perfect divisors of a number in C++
- Print all triplets with given sum in C++
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- All unique triplets that sum up to a given value in C++
- Print triplets with sum less than or equal to k in C Program
- Minimum number of single digit primes required whose sum is equal to N in C++
- Find the largest area rectangular sub-matrix whose sum is equal to k in C++
- Find all triplets in a list with given sum in Python
- Count number of triplets with product equal to given number with duplicates allowed in C++
- How do you find continuous sub array whose sum is equal to a given number in Java?
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given with an array of n integers and the task is to calculate the count all the triplets whose sum is equal to perfect cube

A perfect cube is a number which is a cube of any number, like 125 is a cube of 5 so we can say that 125 is a perfect cube. Some of the perfect cube integers are 1, 8, 27, 64, 125….

So, according to the problem in the array we have to find and count those triplets (set of 3 values) whose sum is equal to a perfect cube number. Moreover the condition provided the sum of the triplet be at most 15000 so there can be only 24 cubes are possible. So we will be using a Dynamic programming approach to solve the problem in less complexity.

Input− array[] = { 5, 2, 18, 6, 3 };Output− Number of Triplets are= 1Explanation− 18+6+3 = 27 (is a perfect cube) Except this no other triplet is a perfect cube.Input− array[] = {1, 2, 3, 4, 5};Output− Number of Triplets are= 2Explanation− 1 + 2 + 5 = 8 (is a perfect cube) 1 + 3 + 4 = 8 (is a perfect cube)

Input the array of positive integers

Calculate its size

Using dynamic programming we will find the occurence of the digits in the array.

Initialise the variable ans to store the count of number of triplets.

Traverse and find the third occurence of the triplet’s set and find whether it is a perfect cube. If the triplet is a perfect cube, increment the value of ans by 1.

Return the ans.

#include <bits/stdc++.h> using namespace std; int arrd[1001][15001]; // Function to find the occurence of a number // in the given range void compute(int ar[], int num){ for (int i = 0; i < num; ++i) { for (int j = 1; j <= 15000; ++j) { // if i == 0 // assign 1 to present value if (i == 0) arrd[i][j] = (j == ar[i]); // else add +1 to current state with // previous state else arrd[i][j] = arrd[i - 1][j] + (ar[i] == j); } } } // Function to count the triplets whose sum // is a perfect cube int countTriplets(int ar[], int num){ compute(ar, num); int ans = 0; // Initialize answer for (int i = 0; i < num - 2; ++i) { for (int j = i + 1; j < num - 1; ++j) { for (int k = 1; k <= 24; ++k) { int cube = k * k * k; int rem = cube - (ar[i] + ar[j]); // count all occurrence of third triplet // in range from j+1 to n if (rem > 0) ans += arrd[num - 1][rem] - arrd[j][rem]; } } } return ans; } // main function code int main(){ int ar[] = { 5, 2, 18, 6, 3 }; int num = sizeof(ar) / sizeof(ar[0]); cout << “Number of Triplets are= ”<<countTriplets(ar, num); return 0; }

If we run the above code it will generate the following output −

Number of Triplets are= 1

Advertisements