Skip to content

Instantly share code, notes, and snippets.

@Lydon-01
Created November 18, 2017 05:59
Show Gist options
  • Save Lydon-01/5afb3a52952d9d3c5e3d949e5b342847 to your computer and use it in GitHub Desktop.
Save Lydon-01/5afb3a52952d9d3c5e3d949e5b342847 to your computer and use it in GitHub Desktop.
Find the smallest value in the parameter array, and find the largest value, and return the largest value minus the smallest value. If the array has length 0, return -1.
/*
* find the biggest and smallest
*/
public int findRange(int[] intArray) {
if(intArray.length == 0){ //check that the array is bigger than 0
return -1;
}
int largest = intArray[0];
int smallest = intArray[0];
for (int x=1;x<intArray.length;x++){ // find the smallest
if (intArray[x] < smallest){
smallest = intArray[x];
}
}
for (int i = 1;i<intArray.length;i++){ // find the largest number
if (intArray[i] > largest){
largest = intArray[i];
}
}
return largest - smallest; //output the difference
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment