Count all triplets whose sum is equal to a perfect cube | C # Code for Count all triplets whose sum is equal to a perfect cube ; Function to calculate all occurrence of a number in a given range ; if i == 0 assign 1 to present state ; else add + 1 to current state with previous state ; Function to calculate triplets whose sum is equal to the perfect cube ; Initialize answer ; count all occurrence of third triplet in range from j + 1 to n ; Driver Code
using System ; class GFG { public static int [ , ] dp ; public static void computeDpArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j <= 15000 ; ++ j ) { if ( i == 0 && j == arr [ i ] ) dp [ i , j ] = 1 ; else if ( i == 0 ) dp [ i , j ] = 0 ; else if ( arr [ i ] == j ) dp [ i , j ] = dp [ i - 1 , j ] + 1 ; else dp [ i , j ] = dp [ i - 1 , j ] ; } } } public static int countTripletSum ( int [ ] arr , int n ) { computeDpArray ( arr , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { for ( int j = i + 1 ; j < n - 1 ; ++ j ) { for ( int k = 1 ; k <= 24 ; ++ k ) { int cube = k * k * k ; int rem = cube - ( arr [ i ] + arr [ j ] ) ; if ( rem > 0 ) ans += dp [ n - 1 , rem ] - dp [ j , rem ] ; } } } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 1 , 20 , 6 } ; int n = arr . Length ; dp = new int [ 1001 , 15001 ] ; Console . Write ( countTripletSum ( arr , n ) ) ; } }