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.
1
Expert's answer
2017-01-25T08:46:03-0500
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: 82)   input      int[] a = {2, 5, -4, 8, 3};      int range = getRange(a);   outputrange is: 123)   input      int[] a = {1, 2, 11, 4}};      int range = getRange(a);   output      range is: 11

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS