aboutsummaryrefslogtreecommitdiff
path: root/max_diff_between_incr_elem.c
blob: bf35ab0510444c9f6f058e83df5915637c8e2d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 2016. Maximum Difference Between Increasing Elements
#include "leetcode.h"

/*
 * given a 0-indexed integer array 'nums' of size 'n', find the maximum
 * difference between 'nums[i]' and 'nums[j]' (ie. 'nums[j] - nums[i]') such
 * that '0 <= i < j < n' and 'nums[i] < nums[j]'. return the maximum difference.
 * if no such 'i' and 'j' exist, return -1
 */

int maximumDifference(int *nums, int numsSize) {
  int min = nums[0], ans = -1;
  for (int i = 1; i < numsSize; ++i) {
    ans = fmax(ans, nums[i] - min);
    min = fmin(min, nums[i]);
  }
  return ans;
}

int main() {
  int n1[] = {7, 1, 5, 4}, n2[] = {9, 4, 3, 2}, n3[] = {1, 5, 2, 10};
  printf("%d\n", maximumDifference(n1, ARRAY_SIZE(n1))); // expect: 4
  printf("%d\n", maximumDifference(n2, ARRAY_SIZE(n2))); // expect: -1
  printf("%d\n", maximumDifference(n3, ARRAY_SIZE(n3))); // expect: 9
}