import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int a_i=0; a_i < n; a_i++){ a[a_i] = in.nextInt(); } //sort the solution using a fast sort (generally, O(n*log(n)), //but since the values are all integers, something like bucket sort or radix sort //could be better. Use Arrays.sort() for now) Arrays.sort(a); //after sorting, all numbers are in order, so just have to now calculate //the absolute difference between the number at a[index] and its neighboring numbers //(a[index-1] and a[index+1]), since they are the closest numbers and therefore //any other number before a[index-1] and after a[index+1] will have a bigger //absolute difference with respect to a[index] int minAbsDist = Math.abs(a[0] - a[1]); //array size is 2 <= size <= pow(10, 5). //Use i = 1 and i < n - 1 b/c we are already accessing a[0] and a[n-1] in loop. //This will prevent out of bounds errors for(int i = 1; i < n - 1; ++i){ int num1 = Math.abs(a[i - 1] - a[i]); int num2 = Math.abs(a[i] - a[i + 1]); int smaller = num1 < num2 ? num1 : num2; if(smaller < minAbsDist){ minAbsDist = smaller; } } System.out.println(minAbsDist); } }