Like people, numbers are also friends with each other.Friendliness between any two numbers a and b is defined as the absolute difference between the two. Lower is this number more friendly the numbers are. Now you are given an array of numbers and you are required to find the friendliness of this array. This can be calculated as the sum of the friendliness of each element in the array with its closest friend in the same array.

Output the value of friendliness for the given array.

**Input:**

The first line of input contains an integer T denoting the number of test cases. Each test case contains the number of elements in the array a[] as n and next line contains space separated n elements in the array a[].

**Output:**

Print an integer which denotes the friendliness in the array.

**Constraints:**

1<=T<=10

2<=n<=10000

1<=a[i]<=100000

**Example:
Input:**

3

3

4 1 5

6

5 10 1 4 8 7

9

12 10 15 22 21 20 1 8 9

5

9

18

**Explanation:**

For test-case 1:

3

4 1 5

Here the elements are 4, 1, and 5.

The friendliness of 4 with other elements are

F(4,1) = |4-1| = 3

F(4,5) = |4-5| = 1

Min(F(4,1),F(4,5)) = 1 => This means 4 is closet friend to 5

The friendliness of 1 with other elemnts are

F(1,4) = |1-4| = 3

F(1,5) = |1-5| = 4

Min(F(1,4),F(1,5)) = 3 => This means 1 is closet friend to 4

The friendliness of 5 with other elemnts are

F(5,1) = |5-1| = 4

F(5,4) = |5-4| = 1

Min(F(5,1),F(5,4)) = 1 => This means 5 is closet friend to 4

So, the total friendliness of the array is Min(F(4,1),F(4,5)) + Min(F(1,4),F(1,5)) + Min(F(5,1),F(5,4)) =>1+3+1 => 5

Author: shashwat jain

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

agness | 122 |

aman1931998 | 102 |

Survivor | 100 |

uchitnitk | 96 |

lonecoder | 95 |

KshatriyaYash | 1066 |

nikhil_sojan | 1032 |

lonecoder | 1030 |

mazumderrohit8 | 899 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4492 |

Quandray | 4444 |

Login to report an issue on this page.