-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoRivalStudents.java
More file actions
47 lines (36 loc) · 912 Bytes
/
TwoRivalStudents.java
File metadata and controls
47 lines (36 loc) · 912 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
38
39
40
41
42
43
44
45
46
47
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class TwoRivalStudents
{
public static void main( String[] args )
{
Scanner s = new Scanner( System.in );
int t = s.nextInt();
while( t-- > 0 )
{
int n = s.nextInt();
int x = s.nextInt();
int a = s.nextInt();
int b = s.nextInt();
System.out.println( getMaxDistance( n, x, a, b ) );
}
}
private static int getMaxDistance( int n, int x, int a, int b )
{
int max = Math.max( a, b );
int min = Math.min( a, b );
int extra = 0;
max = max+x;
if(max>n)
{
extra = max-n;
max = n;
}
min -= extra;
if( min < 1 )
min = 1;
return max - min;
}
}