sgoel01's blog

By sgoel01, history, 10 years ago, In English

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 )

Full text and comments »

  • Vote: I like it
  • +14
  • Vote: I do not like it