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.
// 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;
}
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 →
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; }