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

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

Given an unsorted Integer array with their values. Need to calculate sum of Multiplication of Distance between each pair and The value that is maximum between them.

To be more clear. Lets say array A[] with n elements

Calculate:-


for all i & j (i<j) sum (distance(i,j)*(max(A[i],A[j])))

O(n^2) is easy. I want better than that. I've tried hard but don't know what data structure to use. I would request to give only a hint to solve this problem ( I will try from there )

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

»
10 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

This problem isn't very difficult.
My hint for you: for each i find all pairs if indexes in which the distance if multiplied by a[i]. This can be done effectively.

PS.
Can your give the link to this problem?