Java > Array-2 > bigDiff (CodingBat Solution)

Problem:

Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in Math.min(v1, v2) and Math.max(v1, v2) methods return the smaller or larger of two values.

bigDiff({10, 3, 5, 6}) → 7
bigDiff({7, 2, 10, 9}) → 8
bigDiff({2, 10, 7, 2}) → 8


Solution:

public int bigDiff(int[] nums) {
  int min = nums[0];
  int max = nums[0];
  
  for (int i = 0; i < nums.length; i++){
    min = Math.min(min,nums[i]);
    max = Math.max(max,nums[i]);
  }
  return max-min;

}

2 comments:

  1. public int bigDiff(int[] nums) {
    int min=nums[0];
    int max=nums[0];
    for(int i=0; imax){
    max=nums[i];
    }

    }
    return max-min;
    }

    ReplyDelete
  2. public int bigDiff(int[] nums) {
    int min = nums[0];
    int max = nums[0];
    for(int i = 0; i < nums.length; i++) {
    if(min < nums[i]) min = nums[i];
    if(max > nums[i]) max = nums[i];
    }
    return Math.abs(max - min);
    }

    ReplyDelete