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
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 2 years ago.
Improve this question
Given array A[] and B[], to find how frequently each possible sums A[i] + B[j] appears we can use FFT
For example
A = [1,2,3], B = [2,4].
The sum 3 can be obtained in 1 way, sum 4 : 1 way, sum 5 : 2 ways, sum 6 : 1 way, sum 7 : 1 way
The way we can do this is to construct two polynomials, P and Q with their power corresponding to the element of the array. And apply the regular FFT.
So is there an efficient way of backtracking the numbers that forms the above. To elaborate, we know 3 can be formed in 1 way. But how do we know which two numbers form it?
One way to do it would be the classic two sum algorithm, i.e given an array and a target sum. Find the pairs that create the sum. Which is O(n)
Given we can have N different targets, the resulting algorithm is O(n^2). But I want to keep it under O(nlogn).
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 2 years ago.
Improve this question
Let us assume there is highway
There are N potholes at points [p1, p2, p3...., pn]
There are equal number of service crew at points [s1, s2, s3...., sn]
One service crew can repair one pothole only
The cost to send a crew at point pX to repair a pothole at point sX is |pX-sX|
How would you find the minimum cost to repair the road?
Ex:
Potholes are at [3, 5, 7]
Service Crew are stationed at [1, 3, 5]
Few possible combinations are:
1->3, 3->5, 5->7 (Cost = 6)
1->5, 3->7, 5->3 (Cost = 10)
Share/Explain the algorithm you'd use to solve this problem?
What is the time & space complexity of your algorithm?
In this example it should be 6, no matter what!
This is the problem of finding maximum matching in a fully connected bipartite graph.
If the problem has same number of potholes and crews, it's a perfect matching.
Intention would be to find out maximum matching between potholes P and crews C.
Assumption: A pothole can be serviced by any crew.
Matching condition: |P(i) - C(j)| is minimum.
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 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.
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...