Max difference

4.1

32 votes
Algorithms, , Linear Search, Greedy algorithm
Problem

The weight of an array is defined as the difference between the maximum and minimum element of the array. For example, the weight of the array [3, 7, 1, 2] is (7 - 1) = 6, the weight of the array [2] is (2 - 2) = 0, the weight of an empty array is 0.
You are given an array A of length N. You have to divide the elements of A into two subsequences S1, and S2 (one of S1, S2 can be empty) such that:

  • Each element of A belongs to one of S1 and S2.
  • The sum of weights of the two subsequences is maximum.

Find the maximum possible sum of weights of the two subsequences.

 Input format

  • The first line contains denoting the number of test cases. The description of T test cases is as follows:
  • For each test case:
    • The first line contains denoting the size of array A.
    • The second line contains N space-separated integers A[1], A[2], ....., A[N] - denoting the elements of A.

Output format
For each test case, print the maximum possible sum of weights of the two subsequences.

Constraints

\(1 \leq T \leq 10^4 \\ 2 \leq N \leq 10^5\\ \\1\leq A_i \le 10^9 \\ Sum\; of \; N\;over\;all\;test\;cases\;does\;not\;exceed\;2\cdot 10^5. \)

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Test case 1: One possible division is [1, 3], [2] Here the total weight is = (3 - 1) + (2 - 2) = 2.. 

Editor Image

?