-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxDiff.java
More file actions
49 lines (43 loc) · 1.3 KB
/
Copy pathMaxDiff.java
File metadata and controls
49 lines (43 loc) · 1.3 KB
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
package Array;
/**
* Created by Prashant on 6/12/16.
* Given an array arr[] of integers, find out the difference between any two elements
* such that larger element appears after the smaller number in arr[].
* http://www.geeksforgeeks.org/maximum-difference-between-two-elements/
*
* follow up question: http://www.geeksforgeeks.org/given-an-array-arr-find-the-maximum-j-i-such-that-arrj-arri/
*/
public class MaxDiff {
public static int maxDiff(int[] a) {
int maxDiff= a[1] - a[0];
int min = a[0];
for(int i=1;i<a.length;i++) {
int diff = a[i] - min;
if(diff>maxDiff) {
maxDiff = diff;
}
if (min>a[i]) {
min = a[i];
}
/**
*
*
* in this case for decreasing order it will give zero max diff.
* if (min>a[i]) {
min = a[i];
}
if(diff>maxDiff) {
maxDiff = diff;
}
*
*/
}
return maxDiff;
}
public static void main(String[] args) {
int arr[] = {1, 2, 6, 80, 100};
System.out.print(maxDiff(arr)+" ");
int ar[] = {99, 10, 9, 8, 7, 6, 1};
System.out.print(maxDiff(ar)+" ");
}
}