Maximum Gap | GeeksforGeeks | pytohn

Given an unsorted array. Your task is to find the maximum difference between the successive elements in its sorted form.

Input:
The first line of input contains an integer T,  number of test cases. Following T lines contains an integer N denoting the size of array and next line followed contains the array elements.

Output:
Print the maximum gap between the successive elements.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10
6
1 ≤ A[i] ≤ 10
6

Example:
Input:
1
3
1 10 5

Output:
5

Explanation:
Testcase 1:
 The maximum difference between successive elements of array is 5(abs(5-10)).

 

t = int(input())
for _ in range(t):
    n = int(input())
    arr=list(map(int,input().split()))
    arr.sort()
    max=0
   
    for i in range(n-1):
        j = i+1
        new = arr[j]-arr[i]
        if(new>max):
            max=new
    print(max)


Comments