- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Count all triplets whose sum is equal to a perfect cube in C++

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

## What is the 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.

## For Example

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)

## Approach used in the below program is as follows

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.

## Example

#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; }

## Output

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

Number of Triplets are= 1

- Related Questions & Answers
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Sum all perfect cube values upto n using JavaScript
- Count of pairs in an array whose sum is a perfect square in C++
- Count triplets in a sorted doubly linked list whose product is equal to a given value x in C++
- Count the nodes whose weight is a perfect square in C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Count pairs from two linked lists whose sum is equal to a given value in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count numbers upto N which are both perfect square and perfect cube in C++
- Count quadruples from four sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Maximum Primes whose sum is equal to given N in C++
- Picking all elements whose value is equal to index in JavaScript