Sum of multiplication of distance and maximum value for each pair in an Integer Array

Revision en1, by sgoel01, 2016-12-16 19:57:47

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 )

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English sgoel01 2016-12-16 19:57:47 588 Initial revision (published)