-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptimal_Sorting.java
50 lines (50 loc) · 1.03 KB
/
Optimal_Sorting.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
import java.util.Scanner;
class clas
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,n,x,j,c=0;
n=sc.nextInt();
for(i=0;i<n;i++)
{
int a[],h=9999,b=0;
x=sc.nextInt();
a=new int[x];
for(j=0;j<x;j++)
{
a[j]=sc.nextInt();
}
c=0;
for(j=0;j<x-1;j++)
{
if(a[j]<=a[j+1])
{
c+=1;
}
}
if(c+1==x)
{
System.out.println("0");
}
else
{
for(j=0;j<x;j++)
{
if(a[j]>=b)
{
b=a[j];
}
}
for(j=0;j<x;j++)
{
if(a[j]<=h)
{
h=a[j];
}
}
System.out.println(b-h);
}
}
}
}