PROBLEM 16: Honest Coach
Problem Reference : Codeforces
There are athletes in front of you. Athletes are numbered from to from left to right. You know the strength of each athlete — the athlete number has the strength .
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams and so that the value is as small as possible, where is the maximum strength of an athlete from team , and is the minimum strength of an athlete from team .
For example, if and the strength of the athletes is , then one of the possible split into teams is:
- first team: ,
- second team: .
In this case, the value will be equal to . This example illustrates one of the ways of optimal split into two teams.
Print the minimum value .
The first line contains an integer () — the number of test cases in the input. Then test cases follow.
Each test case consists of two lines.
The first line contains positive integer () — number of athletes.
The second line contains positive integers (), where — is the strength of the -th athlete. Please note that values may not be distinct.
For each test case print one integer — the minimum value of with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Solution:
- t = int(input())
- i = 0
- while i < t:
- n = int(input())
- x = [int(x) for x in input().split()]
- x.sort()
- result = x[n-1]-x[0]
- for a in range(0,n):
- for b in range(a+1,n):
- result = min(result,x[b]-x[a])
- print(result)
- i += 1
5 5 3 1 2 6 4 6 2 1 3 2 4 3 4 7 9 3 1 2 1 1000 3 100 150 200
1 0 2 999 50
No comments:
Post a Comment