DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on • Edited on

1 1

1453. Leetcode Solution in Java

class Solution {
    public int numPoints(int[][] points, int r) {
        int maxPoints = 1;
        int pointsCount = points.length;
        for (int i = 0; i < pointsCount; i++) {
            for (int j = i + 1; j < pointsCount; j++) {
                double[][] intersections = getIntersections(points[i], points[j], r);
                for (double[] intersection : intersections) {
                    int pointsInCircle = 0;
                    for (int[] point : points) {
                        double distance = distance(intersection, point);
                        if (distance <= r + 1e-5)
                            pointsInCircle++;
                    }
                    maxPoints = Math.max(maxPoints, pointsInCircle);
                }
            }
        }
        return maxPoints;
    }

    public double[][] getIntersections(int[] point1, int[] point2, int radius) {
        int squaredDistance = squaredDistance(point1, point2);
        if (squaredDistance > radius * radius * 4)
            return new double[0][2];
        else if (squaredDistance == radius * radius * 4) {
            double[] intersection = new double[2];
            for (int i = 0; i < 2; i++)
                intersection[i] = (point1[i] + point2[i]) / 2.0;
            double[][] intersections = new double[1][2];
            intersections[0] = intersection;
            return intersections;
        } else {
            double[] midPoint = new double[2];
            for (int i = 0; i < 2; i++)
                midPoint[i] = (point1[i] + point2[i]) / 2.0;
            double remaining = Math.sqrt(radius * radius - squaredDistance / 4.0);
            int difference1 = point1[1] - point2[1];
            int difference2 = point2[0] - point1[0];
            double radian = Math.atan(1.0 * difference2 / difference1);
            double[] intersection0 = {midPoint[0] + remaining * Math.cos(radian), midPoint[1] + remaining * Math.sin(radian)};
            double[] intersection1 = {midPoint[0] - remaining * Math.cos(radian), midPoint[1] - remaining * Math.sin(radian)};
            double[][] intersections = new double[2][2];
            intersections[0] = intersection0;
            intersections[1] = intersection1;
            return intersections;
        }
    }

    public int squaredDistance(int[] point1, int[] point2) {
        return (point2[0] - point1[0]) * (point2[0] - point1[0]) + (point2[1] - point1[1]) * (point2[1] - point1[1]);
    }

    public double distance(double[] point1, int[] point2) {
        return Math.sqrt((point2[0] - point1[0]) * (point2[0] - point1[0]) + (point2[1] - point1[1]) * (point2[1] - point1[1]));
    }
}
Enter fullscreen mode Exit fullscreen mode

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay