Saturday, 27 November 2021

Honest Coach Problem !!!

 PROBLEM 16:  Honest Coach


Problem Reference : Codeforces 

There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete — the athlete number i has the strength si.

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 A and B so that the value |max(A)min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.

For example, if n=5 and the strength of the athletes is s=[3,1,2,6,4], then one of the possible split into teams is:

  • first team: A=[1,2,4],
  • second team: B=[3,6].

In this case, the value |max(A)min(B)| will be equal to |43|=1. This example illustrates one of the ways of optimal split into two teams.

Print the minimum value |max(A)min(B)|.


Input

The first line contains an integer t (1t1000) — the number of test cases in the input. Then t test cases follow.

Each test case consists of two lines.

The first line contains positive integer n (2n50) — number of athletes.

The second line contains n positive integers s1,s2,,sn (1si1000), where si — is the strength of the i-th athlete. Please note that s values may not be distinct.


Output

For each test case print one integer — the minimum value of |max(A)min(B)| 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: 


  1. t = int(input())
  2. i = 0
  3. while i < t:
  4. n = int(input())
  5. x = [int(x) for x in input().split()]
  6. x.sort()
  7. result = x[n-1]-x[0]
  8. for a in range(0,n):
  9. for b in range(a+1,n):
  10. result = min(result,x[b]-x[a])
  11. print(result)
  12. i += 1



This solution is in python language.


Example
input
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
output
1
0
2
999
50

No comments:

Post a Comment

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...