-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNebula.java
More file actions
37 lines (37 loc) · 757 Bytes
/
Nebula.java
File metadata and controls
37 lines (37 loc) · 757 Bytes
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
import java.util.*;
class Nebula
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
if(n<3||n>1000)
return;
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=in.nextInt();
int t=in.nextInt();
int best=Integer.MIN_VALUE;
int min=Integer.MAX_VALUE;
Arrays.sort(arr);
for(int i=0;i<n-2;i++)
{
int l=i+1;
int r=n-1;
while(l<r)
{
int sum=arr[i]+arr[l]+arr[r];
int d=Math.abs(sum-t);
if(d<min||(d==min&&sum>best)){
best=sum;
min=d;
}
if(sum<t)
l++;
else
r--;
}
}
System.out.println(best);
}
}