Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
1.1 kB
9
Indexable
Never
import java.util.Arrays;
import java.util.Scanner;

public class F {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();


        long peoples[] = new long[n];
        for (int i = 0; i < n; i++) {
            peoples[i] = scanner.nextLong();
        }
        if (n == 2) {
            System.out.println(diff(peoples[0],peoples[1]));
        } else {

            Arrays.sort(peoples);
            long max = maxDiff(peoples[0], peoples[1], peoples[2]);

            for (int i = 3; i < n; i++) {

                long nmax = 0;
                nmax = minDiff(peoples[i], peoples[i - 1], peoples[i - 2]);
                Math.max(max, nmax);
            }

            System.out.println(max);
        }
    }



    public static long diff(long a,long b){
        return Math.abs(a-b);
    }   public static long maxDiff(long a,long b,long c){
        return Math.max(diff(a,b),diff(a,c));
    } public static long minDiff(long a,long b,long c){
        return Math.min(diff(a,b),diff(a,c));
    }
}
Leave a Comment