for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { rev=rev+abs(s[i]s[j]); } } asked 27 Sep '17, 23:02

you can proceed as follows : This will get you to O(nlogn) from O(n^2) Note that the last term will become zero if the number of items is odd , this is not a problem Explanation : answered 29 Sep '17, 02:00
I don't think it requires $s[i] \ge 0$.
(29 Sep '17, 02:07)
I agree with @meooow because we are using abs
(29 Sep '17, 02:09)
now that I think about it , yeah it does not require s[i] >=0 .. I guess I was just being too cautious :p
(30 Sep '17, 00:06)

if s[i]>=0, you can sort then use prefix sum for(int i=n1;i>0;i)ans+= i *v[i] +pref[i1]pref[0] answered 28 Sep '17, 09:22
