Computing the closest point inside an intersection of half-spaces

Is there an efficient way to compute, given a point, the closest point to it that’s on the inside of an intersection of halfspaces?

For example, given the halfspaces a & b, the closest point to p that’s within the halfspaces is x

.p        ^        |        |        | x    <---.--------------> b        |       +        |        | +        |        v a 

I’m particularly interested in an algorithm for halfspaces in 3D (planes)

My current line of thinking leads me to have to compute intersections of each half space, and then find the shortest point by picking from the shortest distance to each halfspace and each intersection (line, point) that’s within all the halfspaces. Is there a better way?

Closest points of curves on convex surfaces

Let $ \Sigma$ be a convex surface in $ \mathbb{R}^3$ and $ \Gamma \subset \Sigma$ be a closed simple space curve with parameterization $ \gamma : \mathbb{S}^1 \rightarrow \mathbb{R}^3$ . Assume that a functional $ \phi: \mathbb{S}^1 \times \mathbb{S}^1 \rightarrow \mathbb{R}^+$ defined as $ \phi(u_1, u_2) := \Vert \gamma(u_2) – \gamma(u_1) \Vert^2$ has a local minimum at $ (u_1, u_2)$ . I would like to show that tangent vectors $ T(u_1)$ and $ T(u_2)$ must then be collinear.

It is easy to prove that the statement holds true when $ \Sigma$ is a plane or a sphere and that its higher dimensional analogy for curves lying on hyperplanes or hyperspheres in spaces of dimension strictly larger that 3 is false.

What is the closest airport to the center of the city it serves?

Inspired by this, but the complete opposite: What is the most remote airport from the center of the city it supposedly serves?

Personally, I can’t think of anything closer than Adelaide Airport (ADL), only 6.0km from Tarntanyangga to the Terminal Dropoff point (as close as you can get to the terminal with a car). https://goo.gl/maps/JLbaCWcUp4kFTof38

Hong Kong’s old Kai Tak would have been close (depending on where you count the ‘centre’ of Hong Kong to be) but that’s long gone.

Are there any others that even come close?

(only rule I’ll stipulate is that it has to be a decent-sized city 50k+ population, rural towns with 5 houses and a dusty airstrip in their backyards don’t count)

Find all k points which are closest to origin


We have a list of points on the plane. Find the K closest points to the origin (0, 0).

(Here, the distance between two points on a plane is the Euclidean distance.)

You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)

Example 1:

Input: points = [[1,3],[-2,2]], K = 1 Output: [[-2,2]] Explanation:  The distance between (1, 3) and the origin is sqrt(10). The distance between (-2, 2) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]]. 

Example 2:

Input: points = [[3,3],[5,-1],[-2,4]], K = 2 

Output: [[3,3],[-2,4]] (The answer [[-2,4],[3,3]] would also be accepted.)

Note:

1 <= K <= points.length <= 10000 -10000 < points[i][0] < 10000 -10000 < points[i][1] < 10000

Also note that there can be a situation where distance of 2 nodes are equal and hence can print any of the node.

========================================

Here is my approach.

  1. Took tree map (So that I get all sorted distance)
  2. Start filling tree map for all points
  3. Finally took first k element.

    public class KClosestPointsToOrigin {  public int[][] kClosest(int[][] points, int K) {      int rows = points.length;       SortedMap<Double, List<CoordinatePoint>> distanceToPoint = new TreeMap<>();      for(int i =0; i<rows; i++) {         double distance = getDistance(points[i]);         if(Objects.nonNull(distanceToPoint.get(distance))) {             List<CoordinatePoint> coordinatePointList = distanceToPoint.get(distance);             coordinatePointList.add(new CoordinatePoint(points[i][0], points[i][1]));         } else {             List<CoordinatePoint> coordinatePoints = new ArrayList<>();             coordinatePoints.add(new CoordinatePoint(points[i][0], points[i][1]));             distanceToPoint.put(distance, coordinatePoints);// x and y coordinates.         }     }      int[][] arrayToReturn = new int[K][2];     int counter = 0;     for (Double key : distanceToPoint.keySet()) {         List<CoordinatePoint> coordinatePoints = distanceToPoint.get(key);         Iterator iterator1 = coordinatePoints.iterator();         while (iterator1.hasNext() && counter < K) {             CoordinatePoint coordinatePoint = (CoordinatePoint) iterator1.next();             arrayToReturn[counter][0] = coordinatePoint.x;             arrayToReturn[counter][1] = coordinatePoint.y;             counter++;         }     }      return arrayToReturn; }  private double getDistance(int[] point) {     int x = point[0];     int y = point[1];      int x2 = Math.abs(x) * Math.abs(x);     int y2 = Math.abs(y) * Math.abs(y);      return Math.sqrt(x2+y2); } 

    }

    class CoordinatePoint { int x; int y;

    public CoordinatePoint(int x, int y) {     this.x = x;     this.y = y; } 

    }

However, this solution is not efficient as runtime and memory usage is high. Can you please help me to optimize the solution.

find the bitwise and of a subarray closest to given number

It was a question i had been asked on an online assessment for a company please help me in this question-

Given an array,A of size N and an integer P , find the subarray B=A[i…j] such that i<=j , compute the bitwise value of subarray elements say K= B[i]&B[i+1]&..&B[j]. Output the minimum value of |K-P| among all values of K. size of array 1<=A.size<=100000 A[i] 0 to 10^8

i could only think of brute force.

Closest k points – performance on large lists

Very similar to this

Problem formulation: Given a list $ L$ of n points with GPS coordinates and a second list $ Q$ of $ m$ points, find the $ k$ (let’s say 3) closest points on $ L$ for each element on $ Q$ .

Aditional constrains: Assuming that both $ L$ and $ Q$ are very large, and performance is an issue. We can also assume that $ Q$ is much larger thant $ L$ . Preprocessing can be performed, but has to be robust to updates on lists $ L$ and $ Q$ (ergo, it can’t be be very performance heavy).

Problem extensions: If we can’t have preprocessing, because the updates on $ L$ and $ Q$ are very common, what would be the solution?

If you can also point me out to implemented solutions on R or Python it would be great!

How to divide a line of numbers into N groups such that the sums of each group are closest to their mean using dynamic programming?

I have M numbers arranged into a line. I need to divide the line into N groups without changing numbers order such that the sums of the numbers of each group are closest to the mean of these sums by absolute differences.

Example:

Numbers: 1 2 3 4 5 6 7 8 9 10, need to divide into 3 groups.
Let’s say we want to minimize sum of absolute differences (SAD).
Groups: (1) 1 2 3 4 5 6 (sum = 21); (2) 7 8 (sum = 15); (3) 9 10 (sum = 19)
Mean = (21+15+19)/3 = 18.33, SAD = 21-18.33 + 18.33-15 + 19-18.33 = 6.67 <- That’s what we want to minimize.

The question is how to solve it using dynamic programming?


The original question – https://stackoverflow.com/questions/9275280

Why is the closest string problem so important?

The closest string problem has been baffling me. I don’t understand the concept completely, to iterate what I’ve read. The most occurring characters are replaced by a’s and the 2nd b’s and the third c’s.

More formally, given n length-m strings s1, s2, …, sn, the closest string problem seeks for a new length-m string s such that d(s,si) ≤ k for all i, where d denotes the Hamming distance, and where k is as small as possible.[2] A decision problem version of the closest string problem, which is NP-complete, instead takes k as another input and questions whether there is a string within Hamming distance k of all the input strings.1

wikipeida-link

u|v|w|x

x|u|w|v

x|v|w|v


b|a|a|a

a|b|a|b

a|c|a|c

The second group there seems that starting at the first row is a hamming distance of two and the 2nd row has a hamming distance of 1 and skipping the third one a hamming distance of one.

I’m confused, because why don’t they look at the strings horizontally and that two rows of strings are exactly the same.