[interesting question] find an element which is sum of two other elements
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am discussing with my friends about an interesting question about find the maximum element in a set, which is the sum of two other elements.
For example, in set {1, 2, 3, 5, 8, 10}, the answer is 10 (10 = 8 + 2),
which is the maximum element we can find, and it is the sum of two
other elements (8 and 2) in the set.
Currently, I only have brute-force solution. Sorting the set, which takes O(nlgn) time, then enumerate them one by one to find whether two elements can sum up to the maximum element (if the most maximum element does not meet the condition, move to the second largest one), which takes O(n^2) time, so the total time complexity is O(n^2).
I don't know for sure, but my idea would be to have a set with all permutated results (all possible add combinations) and keep this for your checks. Not sure if that would help, really.
This is probability Combinations.. so....
BigTheta( N!/(N-R)!) ?
Depends on how large N is...? In this case, 6!/(6-2)! => 6*5 = 30. So the other case was O(N^2) = 6^2 = 36, thus I think it saves some time on the check, in this example. But it could become larger as more checks are necissary. You will need to iterate through all combos found for each max found. Sort of a O(NM). Where M = BigTheta( N!/(N-R)!).
I believe that the formula gave was for permutations and as you said you only need combinations, so your Big Oh would be (N!/(N-R)!R!) for R = 2 that will give you N(N-1)/2 which is significantly better than N^2, although the Big Oh will be the same.
But I agree with the approach of sort the original list, calculate the combinations, compare.
But that should be BigTheta, I think, as that is the asymptotic lower, and upper bound. There is no worst or best case of combination variance, there only is X number of combos, which will not change (N held constant). So f(C) will always take C!/[(C-2)!2!], best and worst case.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.