Brute forcing closest pair of points?


What is n, i and k here?

This is solved. Easy one.

Feel free to post the solution in case someone else stumbles upon this in the future :slight_smile:

import java.util.*;  
  
public class Example {  
    public static void main(String[] args) {  
        int[][] points = {  
                {1, 1},  
                {2, 2},  
                {3, 3}  
        };  
        double dist = 0;  
        int p1 = 0;  
        int p2 = 0;  
        double mindist = 999999999;  
        for (int i = 0; i < points.length; i++) {  
            for (int j = i + 1; j < points.length; j++) {  
                dist = distance(points[i], points[j]);  
                if (dist < mindist) {  
                    mindist = dist;  
                    p1 = i;  
                    p2 = j;  
                }  
            }  
        }  
        System.out.println(mindist);  
        System.out.println(points[p1][0] + "," + points[p1][1] + " and " + points[p2][0] + "," + points[p2][1]);  
  
    }  
  
    public static double distance(int[] a, int[] b) {  
        return Math.sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]));  
    }  
  
}
1 Like