forked from jaspreetkaur96/CodingQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximum Difference.java
77 lines (64 loc) · 1.53 KB
/
Maximum Difference.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
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
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int tests = sc.nextInt();
for(int t = 0 ; t < tests ; t++)
{
int size = sc.nextInt();
int max_diff = Integer.MIN_VALUE;
int a [] = new int[size];
for(int i = 0 ; i < size ; i++)
{
a[i] = sc.nextInt();
}
int i = 0;
while(i < size)
{
for(int j = i+1 ; j < size ; j++)
{
int curr = a[j] - a[i];
if(curr < 0)
{
break;
}
max_diff = Math.max(max_diff , curr);
}
i++;
}
System.out.println(Math.max(-1,max_diff));
}
}
}
/* Alternative
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int tests = sc.nextInt();
for(int t = 0 ; t < tests ; t++)
{
int size = sc.nextInt();
int max_diff = Integer.MIN_VALUE;
int a [] = new int[size];
for(int i = 0 ; i < size ; i++)
{
a[i] = sc.nextInt();
}
int curr_diff = 0;
for(int i = 0 ; i < size - 1 ; i++)
{
curr_diff = Math.max(curr_diff + a[i+1] - a[i], 0);
max_diff = Math.max(max_diff, curr_diff);
}
if(max_diff == 0) max_diff = -1;
System.out.println(max_diff);
}
}
}
*/