-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmax_diff_btwn_2_elements.c
79 lines (74 loc) · 1.95 KB
/
max_diff_btwn_2_elements.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Date: 2018-10-07
*
* Description:
* Find maximum difference between 2 elements in a array such that larger
* element appears at right of smaller element.
*
* This is also stock buy sell problem to maximize profit when only one
* transaction is allowed.
*
* Approach:
* - Take an initial diff between first 2 elements.
* - Scan array and keep track of smallest element appeared so far.
* - With each new element calculate its diff with smallest element, if diff is
* more than previously calculate then update max diff.
*
* Complexity:
* O(N)
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int n = 0;
int *a = NULL;
int max_diff = 0;
int min_element = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
a = (int *)malloc(sizeof(int)*n);
for (i = 0; i < n; i++) {
printf("Enter element [%d] : ", i);
scanf("%d",&a[i]);
}
max_diff = a[1] - a[0]; // Initial diff
min_element = a[0];
for (i = 1; i < n; i++) {
if (a[i] - min_element > max_diff)
max_diff = a[i] - min_element;
if (min_element > a[i])
min_element = a[i];
}
printf("Max difference between 2 elements(larger appearing later): %d\n",
max_diff);
return 0;
}
/*
* Output:
* -------------------------------------------------
* Enter number of elements: 5
* Enter element [0] : 1
* Enter element [1] : 2
* Enter element [2] : 3
* Enter element [3] : 4
* Enter element [4] : 5
* Max difference between 2 elements(larger appearing later): 4
*
* Enter number of elements: 3
* Enter element [0] : 3
* Enter element [1] : 2
* Enter element [2] : 1
* Max difference between 2 elements(larger appearing later): -1
*
* Enter number of elements: 8
* Enter element [0] : 5
* Enter element [1] : 4
* Enter element [2] : 6
* Enter element [3] : 7
* Enter element [4] : 2
* Enter element [5] : 5
* Enter element [6] : 7
* Enter element [7] : 9
* Max difference between 2 elements(larger appearing later): 7
*/