Блог пользователя wish_me

Автор wish_me, история, 9 лет назад, По-английски

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.

  • Проголосовать: нравится
  • -12
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

The maximum possible value of a[i]+a[j]+a[k] can be 15000. There are 24 perfect cubes less than 15000. You can check if there exists (i,j,k) for a given perfect cube in O(n*n) using two pointers. The overall complexity will be O(24*n*n).