code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Maximumdifference

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class MaximumDifference {

  static int func(int[] nums){
    int max = nums[1] - nums[0];
    int min = nums[0];
    for(int j = 1; j < nums.length; j++){
      max = Math.max(nums[j] - min, max);
      min = Math.min(nums[j], min);
    }
    return max;
  }

  public static void main(String[] args){
    int[] nums = {2, 3, 10, 6, 4, 8, 1};
    int result = func(nums);
    System.out.println(result);
  }
}

Created 2021-10-28T01:03:41+01:00, updated 2021-12-21T03:41:59+00:00 · History · Edit