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= 1
Explanation − 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= 2
Explanation − 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

 Live Demo

#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

Updated on: 15-May-2020

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements