• + 3 comments

    Java solution - passes 100% of test cases

    O(n) runtime achieved by keeping track of minimum and maximum values.

    From my HackerRank solutions.

    class Difference {
        private int[] elements;
        public int maximumDifference;
        Difference (int [] elements) {
            this.elements = elements;
        }
    
        void computeDifference() {
            int min = elements[0];
            int max = elements[0];
            for (int num : elements) {
                min = Math.min(min, num);
                max = Math.max(max, num);
            }
            maximumDifference = max - min;
        }
    } // End of Difference class
    

    Let me know if you have any questions.