• + 0 comments
    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    import java.util.*;
    
    class Result {
    
        /*
         * Complete the 'minimumLoss' function below.
         *
         * The function is expected to return an INTEGER.
         * The function accepts LONG_INTEGER_ARRAY price as parameter.
         */
    
        public static int minimumLoss(List<Long> price) {
            int n = price.size();
            long[] priceArray = new long[n];
            for (int i = 0; i < n; i++) {
                priceArray[i] = price.get(i);
            }
    
            // Create a map to store the original indices of prices
            Map<Long, Integer> indexMap = new HashMap<>();
            for (int i = 0; i < n; i++) {
                indexMap.put(priceArray[i], i);
            }
    
            // Sort the prices in descending order
            Arrays.sort(priceArray);
    
            long maxLoss = Long.MAX_VALUE;
    
            // Iterate through the sorted prices and find the maximum loss
            for (int i = 1; i < n; i++) {
                if (indexMap.get(priceArray[i]) < indexMap.get(priceArray[i - 1])) {
                    maxLoss = Math.min(maxLoss, priceArray[i] - priceArray[i - 1]);
                }
            }
    
            return (int) maxLoss;
        }
    }
    
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            int n = Integer.parseInt(bufferedReader.readLine().trim());
    
            List<Long> price = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Long::parseLong)
                .collect(toList());
    
            int result = Result.minimumLoss(price);
    
            bufferedWriter.write(String.valueOf(result));
            bufferedWriter.newLine();
    
            bufferedReader.close();
            bufferedWriter.close();
        }
    }