How to find the Largest Difference in an Array

How to find the Largest Difference in an Array

·

1 min read

Example

[7,1,5,3,6] -> 6 (7-1)

JAVA code

    public static int getMaxDifferNumFromArray(int[] input) {
        return getMaxFromArray(input) - getMinFromArray(input);
    }
    public static int getMaxFromArray(int[] input) {
        int max = Integer.MIN_VALUE;
        for (int temp : input) {
            if (max < temp) {
                max = temp;
            }
        }
        return max;
    }
    public static int getMinFromArray(int[] input) {
        int min = Integer.MAX_VALUE;
        for (int temp : input) {
            if (min > temp) {
                min = temp;
            }
        }
        return min;
    }