Lanterns

2

1 votes
Greedy Algorithms
Problem

Ashish has a secret servant named Hsrada.
Hsrada has N lanterns. Each lantern has a brightness B value associated with it. Consider all K-sized subsets of these lanterns, among all those subsets, find the maximum ambience value of the subset.
The ambience value of a subset = BiBj i,jsubset,where i<j.

Task
Find the maximum ambience value.

Notes

  • The ambience value of a subset with 1 element is 0.
  • | X - Y | represents the absolute value of (X - Y).

Example
Assumptions

  • K = 2
  • N = 3
  • B = [11, 3, 15]

Approach

  • If we choose the subset [3, 15]. We get an ambience value of ( | 3 - 15 | ) 12.
  • If we choose the subset [11, 3]. We get an ambience value of ( | 11 - 3 | ) 9.
  • If we choose the subset [11, 15]. We get an ambience value of ( | 11 - 15 | ) 4. 

Thus, we get the maximum ambience value of 12.

Input Format
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains T representing the number of test cases.
  • For each test case:
    • The first line contains K representing the number of lanterns to be chosen.
    • The second line contains N representing the number of lanterns.
    • The third line contains N integers, representing the brightness B value of the lanterns.

Output Format
For each test case, print a single integer representing the maximum ambience value.

Constraints
1T10
1KN2105
1Bi109

Sample Input
1
3
4
12 45 1 44
Sample Output
88
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The number of test cases T = 1,

For the first Test Case,
Assumptions

  • K = 3
  • N = 4
  • B = [12, 45, 1, 44]

Approach

  • If we choose the subset [12, 45, 1]. We get an ambience value of 88.
  • If we choose the subset [12, 45, 44]. We get an ambience value of 66.
  • If we choose the subset [45, 1, 44]. We get an ambience value of 88

Thus, we get the maximum ambience value of 88.

Editor Image

?