• + 0 comments

    Two solutions, one as intended and one smarter solution based off of the constraints

    `
    Difference(int[] elements){ this.elements = elements; this.maximumDifference = Integer.MIN_VALUE; }

    // Add your code here
     int computeDifference(){
        for (int i = 0; i < elements.length; i++){
            for(int j = 0; j< elements.length;j++){
                int currentDiff = Math.abs(elements[i] - elements[j]);
                if (maximumDifference < currentDiff){
                    maximumDifference = currentDiff;
                }
            }
        }
        return maximumDifference;
    }
    
    Difference(int[] elements){
        this.elements = elements;
        this.maximumDifference = Integer.MIN_VALUE;
    }
    
    // Add your code here
     int computeDifference(){
        int min = elements[0];
        int max = elements[0];
        for (int num : elements) {
            if (num < min) min = num;
            if (num > max) max = num;
        }
        maximumDifference = max - min;
        return maximumDifference;
    }