ABSP1 - abs(a-b) I

no tags 

Recently Mr. Kopa Samsu is learning programming. On a very renowned online judge, he found a problem:

You are given an array of N numbers in non-decreasing order. You have to answer the summation of the absolute difference of all distinct pairs in the given array.

Do you know what distinct pair means? Suppose you have an array of 3 elements: 3 5 6

Then all the distinct pairs are:

3 5
3 6
5 6

For this problem, Mr. Kopa Samsu implemented an algorithm to find the summation of the absolute difference of all distinct pairs. His algorithm was:

int ABS(int a[], int n)
{
    int sum = 0;
    for (int i = 1; i <= n ;i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            sum += abs(a[i] - a[j]);
        }
    }
}

After a great hard work, he finished the code. But alas!!! Frustration came around him when he submitted his code, the judge gave the verdict “TLE” (Time Limit Exceeded). “How can I get rid of TLE?” he thought a lot but couldn't find any way. Then suddenly, he remembered about you that you (his friend) is very good at programming. So, he came to you seeking your help. Can you help him solving this problem?

Input

The input data set starts with an integer T (T <= 1000), the number of test cases. Then every test case starts with a integer N (N <= 10000), the number of elements in the array. After that, the next line contains N integers A[i], where 1 <= i <= N and 1 <= A[i] <= 1000000000 & A[i] <= A[i+1].

Output

Every test case should output an integer “X”, where X is the summation of the absolute difference of all the distinct pair.

Example

Input:
3
3
1 2 3
3
1 4 5
3
2 4 6

Output:
4
8
8

Problem Set: S.M. Shaheen Sha, Raihat Zaman Neloy

Data Set & Solution: Raihat Zamane Neloy


hide comments
ROHIT Kumar: 2015-09-23 14:20:16

nice question,
just try to do it on paper by taking examples of at least 4 elements and find log(n) solution
u will get it :)

hodobox: 2015-09-06 21:19:12

Test case:
1
5
1 5 8 8 14

58

ANIKET: 2015-08-31 05:07:45

NOTE: Pairs are not distinct

pvsmpraveen: 2015-08-29 11:27:03

Got AC After 3 WA, dont use unsigned long long int , it costed me 3WA maybe "1<=A[i]<=1000000000" question is misleading, I think test cases have negative numbers.... Got AC After changing unsigned long long int to long long int

Last edit: 2015-08-29 11:33:01
venkatesh: 2015-08-12 07:42:52

He says distinct pairs but it doesn't seem like it. Maybe what he meant to say was UNORDERED pairs.
modern authors only call {a, b} an unordered pair if a ≠ b

Last edit: 2015-08-12 07:43:25
shantanu tripathi: 2015-07-28 20:05:29

accepted in one go!! :)

Vaporeon: 2015-06-01 18:17:07

The problem is superb.. :D Just ignore the word 'distinct'. AC using PYPY :))

Ajay Aneja: 2015-05-30 13:16:32

@Nikhil Sheoran:
second case to be considered.question not clear :/

Nikhil Sheoran: 2015-05-25 19:27:52

@Someone who got AC
Please tell which all pair u considered for
Let us say : 1 2 2 4 4
Either (1,2) (1,4) (2,4)
or (1,2) (1,2) (1,4) (1,4) (2,4) (2,4)

Amogh: 2015-05-20 17:06:18

Author says distinct pairs but it doesn't seem like it. Maybe what he meant to say was UNORDERED pairs.Please consider this in case of WA.


Added by:Raihat Zaman Neloy
Date:2014-01-25
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64