Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose you have some points in 3D. You would like to sort it either increasing or decreasing order. You don't consider CW/CCW on sorting. How do you sort?
One method of sorting 3D points would be to compare their magnitudes - their distance from the origin point (0, 0, 0) - which may be computed using a 3D analogue of Pythagora's Theorem:
M = sqrt(x^2 + y^2 + z^2) http://www.sciweavers.org/upload/Tex2Img_1392933041/eqn.png
You'll then have a list of floats/doubles that may be sorted using any conventional sorting algorithm.
That's just the most common method, though. There exist infinitely many ways of comparing 3D points, some of which are more sensible than others. For example, which is the "bigger" point, (1, 0, 0) or (-10, -50, 5)? Comparing the X or Y coordinate would suggest the former being larger, while comparing the Z coordinate or magnitude suggests the latter being larger. None of these answers are completely right or wrong; it really depends on what you need your application to do.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have n (about 10^5) points on a hypersphere of dimension m (between 10^4 to 10^6).
I am going to make a bunch of queries of the form "given a point p, find the closest of the n points to p". I'll make about n of these queries.
(Not sure if the hypersphere fact helps at all.)
The simple naive algorithm to solve this is, for each query, to compare p to all other n points. Doing this n times ends up with a runtime of O(n^2 m), which is far too big for me to be able to compute.
Is there a more efficient algorithm I can use? If I could get it to O(nm) with some log factors that'd be great.
Probably not. Having many dimensions makes efficient indexing extremely hard. That is why people look for opportunities to reduce the number of dimensions to something manageable.
See https://en.wikipedia.org/wiki/Curse_of_dimensionality and https://en.wikipedia.org/wiki/Dimensionality_reduction for more.
Divide your space up into hypercubes -- call these cells -- with edge size chosen so that on average you'll have one point per cube. You'll want a map from hypercells to the set of points they contain.
Then, given a point, check its hypercell for other points. If it is empty, look at the adjacent hypercells (I'd recommend a literal hypercube of hypercells for simplicity rather than some approximation to a hypersphere built out of hypercells). Check that for other points. Keep repeating until you get a point. Assuming your points are randomly distributed, odds are high that you'll find a second point within 1-2 expansions.
Once you find a point, check all hypercells that could possibly contain a closer point. This is possible because the point you find may be in a corner, but there's some closer point outside of the hypercube containing all the hypercells you've inspected so far.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
As we know, with the previous three positive integers (a < b < c), if it is three sides of the triangles are called Pythagorean Triple.
Given integers a. How to find the remaining two of the three Pythagorean quickly.
However, if there are multiple answers, the output b, c largest.
example:
input
9
output
40 41
(9,12,15), (9, 40, 41) are 2 answers. but is the most accurate (9, 40, 41).
I do not know how to solve optimization. Please help me.
Thank you very much.
As
a² = c²-b²=(c-b)*(c+b)
you can try the factors of a² from smallest to largest, as b, c are largest if b+c is largest and thus if c-b is smallest. For odd a, one easily sees that
c = (a²+1)/2
b = (a²-1)/2
give integers with the required property. For even a, the smallest workable factor where the sum of the two factors is even is 2, resulting in
c = (a²+4)/4
b = (a²-4)/4
This simple formula even has an old name: The Platonic sequence
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Imagine a straight line through the origin. Since rotations and
reflections are easy, assume the slope is in the range 0 to 1.
We have a grid of the integer points in the cartesian plane.
I want to find the grid point greater than 0 and <= D the
line passes closest to.
The simple approach is for each x from 1 .. D, find the point above and
below the line and calculate the perpendicular distance to the line.
This will take 2 x D comparisons to find the minimum.
That's not bad but I am trying to come up with a log(D) approach.
Is there one?
An equivalent problem would be to find the closest rational
number n / d where d <= D.
This question seems to be equivalent to yours: Finding the closest integer fraction to a given random real
The accepted answer there uses a Farey Sequence.
Also links to this interesting blog post.
Not a full answer, but an optimization in some cases: If the slope of the line is a rational number, then there will be repetition, allowing you to look at fewer points if D is larger than the denominator.
Eg: if the slope is 12/17, then you don't need to look at more than 17 points out from the origin. After that it will repeat.
Of course, if D < 17 in that example, it's no benefit.
Also, if your slope is π, you're out of luck...
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Studying up for interviews and going through these questions.
8.Given a cube of size n*n*n (i.e made up of n^3 smaller cubes), find the number of smaller cubes on the surface. Extend this to k-dimension.
I don't understand the question very well. For one side, we'll have n^2 number of cubes on the surface. And there are 6 sides on a cube, so is the answer just 6n^2? That seems too simple to me. Any help would be appreciated.
If you have a cube that is 4x4x4, so it would contain 64 cubes of size 4, how many of these cubes are on the surface?
I would do it for 3 and 4, then come up with an equation, and test with 5.
Basically, how many of the cubes don't touch the surface is another way to look at it.
There are n^2 1*1*1 cube in one side, but there are (n-1)^2 2*2*2 cube in each side and ... 3*3*3, ...