Distant Pairs

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    static class Pair{
        int a,b;
        Pair(int a, int b){
            this.a = a;
            this.b = b;
        }
    }
    static int circleDist(int a, int b,int c){
    
        int d = Math.abs(a-b);
        return Math.min(d, c-d);
    }
    

    static int pairDist(Pair p1, Pair p2, int c){

        int d1 = circleDist(p1.a, p2.a, c); 
        int d2 = circleDist(p1.a, p2.b, c);
        int d3 = circleDist(p1.b, p2.a, c);
        int d4 = circleDist(p1.b, p2.b, c);
        return Math.min(Math.min(d1, d2), Math.min(d3, d4));
    

    }

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
        int n = Integer.parseInt(firstMultipleInput[0]);
    
        int c = Integer.parseInt(firstMultipleInput[1]);
    
        List<Pair> pair = new ArrayList<>();
    
         for(int i=0; i<n; i++){
            String[] line = bufferedReader.readLine().split(" "); 
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);
            pair.add(new Pair(a, b));
            //points.add(new Pair(b, i));
         }          
    
        int maxDist = 0;
        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                int d = pairDist(pair.get(i), pair.get(j), c);
                maxDist = Math.max(maxDist, d);
            }
    
        }
        System.out.println(maxDist);
        bufferedReader.close();
    
     }
    

    }