We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classDifference{privateint[]elements;publicintmaximumDifference;Difference(int[]elements){this.elements=elements;}voidcomputeDifference(){intmin=elements[0];intmax=elements[0];for(intnum: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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 14: Scope
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
O(n) runtime achieved by keeping track of minimum and maximum values.
From my HackerRank solutions.
Let me know if you have any questions.