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
Dhruv k14: 2015-03-31 22:44:44

"............ an algorithm to find the summation of the absolute difference of all distinct pairs",says the problem statement; really if duplicate elements are allowed then what is the significance of distinct pairs? Ambiguous problem statement,could not have solved without clarification received from comments

Shashank Garg: 2015-03-13 18:47:18

NOTE: Pairs are not distinct

Sam: 2015-02-28 10:33:24

@Sampath completely agree with you..cost me too many wa's...whats the point of distint pairs then?

:.Mohib.:: 2015-02-19 17:23:36

Really Enjoyed...Awsm problem... :)

Sampath Ravolaparthi: 2015-01-09 17:50:17

GR8!! Did'nt know the same pair can be repeated
could have at least given it in one test case

Pritish Jain: 2014-12-20 09:44:49

good question write the sum of absolute difference and look for pattern

Maroof: 2014-12-14 08:13:21

use long long int. gave WA for int

Abhilash: 2014-12-06 20:14:15

Process on the FLY and output!!

Sudarshan K: 2014-09-30 23:29:19

Note: Use long long (ignore previous comments, int will give you WA) and duplicate elements shouldn't be ignored.

Siya: 2014-09-26 15:02:29

AC in one go :) :) nice question o(n) @invader the answers will be 0 and 6

Last edit: 2014-09-26 15:06:32

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