forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindMaxDifference.java
43 lines (34 loc) · 1.28 KB
/
FindMaxDifference.java
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
package arrays;
public class FindMaxDifference {
/*
* Find the maximum difference between the values in an array such that the largest values always comes after the
* smallest value
* Input: {2, 3, 10, 6, 4, 8, 1}
* Output: 8
* Step 1) Set min value as arr[0] -> a
* Step 2) set max difference as arr[1] - arr[0]; -> b
* Step 3) Iterate and check if the value is > minValue.
* Step 4) If it is greater, then get the difference between the two values & check if the difference > max difference (b). If the difference is > set this diff as max difference
* Step 5) If it is less than minValue, set this value as minValue
* */
public static int findMaxDistance(int[] arr) {
if(arr.length == 1) return arr[0];
int maxDiff = arr[1] - arr[0];
int minValue = arr[0];
for(int value : arr) {
if(value < minValue) {
minValue = value;
} else {
int diff = value - minValue;
if(diff > maxDiff) {
maxDiff = diff;
}
}
}
return maxDiff;
}
public static void main(String[] args) {
int[] arr = {2, 3, 10, 6, 4, 8, 1};
System.out.println(findMaxDistance(arr));
}
}