# Answer to Question #14898 in Java | JSP | JSF for Kaze

Question #14898

Write the body of method getRange:

public static int getRange(int[] a){

which takes an array of integers as parameter and counts the range from the array. For example, if the parameter array holds values {2,5,-4,8,3}, the getRange method returns 12.

public static int getRange(int[] a){

which takes an array of integers as parameter and counts the range from the array. For example, if the parameter array holds values {2,5,-4,8,3}, the getRange method returns 12.

Expert's answer

`public static int getRange(int[] a) {`

int min = a[0];

int max = a[0];

for (int i = 0; i < a.length; i++) {

if (min > a[i]) {

min = a[i];

} else {

if (max < a[i]) {

max = a[i];

}

}

}

if (max < 0 && min < 0) {

return min * -1;

}

if (min < 0) {

min *= -1;

return!min!+!max;

}

return max - min + 1;

}

Examples

`1)`

input

int[] a = {-8, -3, -2, -1};

int range = getRange(a);

output

range is: 8

2)

input

int[] a = {2, 5, -4, 8, 3};

int range = getRange(a);

output

range is: 12

3)

input

int[] a = {1, 2, 11, 4}};

int range = getRange(a);

output

range is: 11

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment