Combination of numbers that equal a given sum java

Forums New posts Search forums. What's new New posts New Excel articles Latest activity. New posts. Excel Articles Latest reviews Search Excel articles. Log in Register. Search titles only. Search Advanced search…. Search forums. Log in. If you forgot your password, you can reset your password. JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding. Thread starter mcgerks Start date Dec 18, Joined Mar 24, Messages Thanks for the help.

Some videos you may like. Excel Facts. How to show all formulas in Excel? Click here to reveal answer. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards. Gerald Higgins Well-known Member. Joined Mar 26, Messages 9, Hi mcgerks. How can you possibly achieve this?

You could arrive at 13 in at least two ways. Joined May 17, Messages 9, The solver can do this sort of stuff. Suggest you google for examples - I have seen something like you're after but unfortunately I can't locate it now.

Thanks Fazza the solver add-in did the trick!

Combinational Sum

Joined May 28, Messages 11, Peter Compton New Member.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I have an array with positive integers in random order. A number x from the list is given ,we need to find any two numbers in the list having sum equal to x.

Now for the worst case I am little confuse is that approach is good? To solve it in O nlognthink what happens if you sort the data, given a number arr[i]can you find efficiently if there is a number x-arr[i] in the now sorted array? You can even enhance the above to O n average case by placing the elements in a hash-set, and now, given an number ycan you find efficiently if x-y is also in the set? Here is O n solution for finding the first pair of indices of array which sums to the expected target.

The solution will stop when it finds the first 2 indices that sum to target, if you need all the pairs that add up to target then instead of using int[] result, you can use ArrayList or even a Map, process the complete array and return it with all the pairs of indices. There is an obvious assumption that the Map's hashcode function is really good and there are not much collisions so that map operations perform in O 1 time.

Sort the array using merge sort Time complexity: n logn. Learn more. Sum of two numbers equal to the given number Ask Question. Asked 4 years, 11 months ago. Active 2 years, 9 months ago. Viewed 4k times. Duplicate of stackoverflow. This is not a dupe. The question is related, but the OP has a different quesiton here. He has a specific different solution, and asks what its complexity. Active Oldest Votes. Then you can solve the problem by using two index variables and doing a single iteration over the numbers.

IvayloStrandjev True, but 1 this is a different question, which I am not answering. SSH Your approach is still wrong for small corner case that can be handled thoughand is still inefficient.

combination of numbers that equal a given sum java

See editted answer. Here you go, Sort the array using merge sort Time complexity: n logn. Overall time complexity: n logn.This article, I will talk about some methods to find which cells sum up to a specific value in Excel.

Find and list all combinations that equal a given sum quickly and easily in Excel. Kutools for Excel 's Make Up A Number utility can help you to find and list all combinations and specific combinations that equal to a given sum number quickly and easily. Click to download Kutools for Excel! Kutools for Excel : with more than handy Excel add-ins, free to try with no limitation in 30 days.

Download and free trial Now! First, you need to create some range names, and then apply an array formula to find the cells that sum to the target value, please do with the following step by step:.

combination of numbers that equal a given sum java

Select the number list and define this list a range name-- Range1 into the Name Boxand press Enter key to finish the range name defined, see screenshot:. After creating the range names, please apply the following array formula into cell B If you are confused with above method, Excel contains a Solver Add-in feature, by using this add-in, you can also identify the numbers which total amount equals a given value. Then in the To section, select Value Ofand enter your target value as you need.

Under the By Changing Variable Cells section, please click button to select cell range B2:B9 where will mark your corresponding numbers. And then click Add button to go to the Add Constraint dialog box, click button to select cell range B2:B9and select bin from the drop down list, see screenshot:. Click OK to go back the Solver Parameter dialog, then click Solve button, some minutes later, a Solver Results dialog box is popped out, and you can see the combination of cells which equal a given sum are marked as 1.

See screenshot:. Note : This method is also only can get one combination cells if there are more than one combination of values has a sum equal to the specific value. The first two methods are all complex for most of our Excel users, here, I can create a VBA code to solve this job quickly and easily. To get the correct result, you must sort the number list in descending order first. And then do with the following steps:. VBA code: Find cells combination that equal a given sum:.

Maybe all of the above methods are somewhat difficult for you, here, I will introduce a powerful-tool, Kutools for Excelwith its Make Up A Number feature, you can quickly get all combinations that equal to a given sum.

Tips: To apply this Make Up A Number feature, firstly, you should download the Kutools for Exceland then apply the feature quickly and easily. After installing Kutools for Excelplease do as this:.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time.

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. The sum indicates that it takes the sum of f x 1 ,x 2I have an idea for the solution, but it is a terribly ineffective algorithm using binary, and it's O 2 n.

Of course, I cannot use "for" because it must be used for n non-specific times.

find a combination of numbers that equal a given sum

The way to solve this is to think of it as an attempt to find all combinations of n numbers between min and max. Adding them up as you go. You can imagine n stones on a number line between min and maxthen moving the furthest right stone until it is at max, when it is you move the next furthest right stone up one place and move all stones to its right. Learn more. Calculate Sum of numbers with all possible combination [closed] Ask Question. Asked 6 years, 6 months ago. Active 6 years, 6 months ago.

Viewed 2k times. Is there anyone who knows more effective algorithm for this? Denim Datta 3, 3 3 gold badges 24 24 silver badges 53 53 bronze badges. StackExchange Math. StackExchange 1 1 silver badge 8 8 bronze badges. Could you explain a little more what this algorithm is supposed to do.

Perhaps in sudo code. StackExchange Sep 30 '13 at Active Oldest Votes. Adding them up as you go You can imagine n stones on a number line between min and maxthen moving the furthest right stone until it is at max, when it is you move the next furthest right stone up one place and move all stones to its right. Richard Tingle Richard Tingle Thank you.Forum Rules. Help Forgotten Your Password? Remember Me?

Subscribe to RSS

Results 1 to 14 of How do I determine which numbers in a list equal a given sum? Register To Reply. RE: How do I determine which numbers in a list equal a given sum? Ken Wright. Re: How do I determine which numbers in a list equal a given sum? If you are looking for a solution Not necessarily the only one to a subset of a group of numbers that will add up to a target number, then this can often be done with Solver. Then, using the range selector under the 'By Changing cells' section, select cells B1:B30 as the ones to change and hit enter which will take you back to the first dialog box.

Now hit the 'Add' button, and add the constraint that B1:B30 must be 'bin' Means binary as in 1 or 0, and it's one of the dropdowns, so just hit the arrow and select 'bin' and just hit Solve. You MUST ensure that in this example, when you add the 'bin' constraint range, you do not inadvertantly include the formula cell B31, else you will get an error message such as 'Binary Contsraint cell reference must include only adjustable cells' Won't do any more than single solution, but for a Finance Dept that will often suffice in this context.

Saves having to change any values in Solver that way, just type what you want in B Thanks, in advance, for any help. See my response as that is exactly what it does -- Regards Ken Again, any help from anyone will be greatly appreciated. Ken, Very impressive idea; excellent exploitation of the 'Solver' utility!

One may randomize the positions of the data A1:A30 in your example and repeat the solver procedure to get multiple solutions. Of course, it would be a cumbersome process since it is impossinle to guess how many solutions are possible for a given scenario. Regards, B.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array. How can I improve this code to reduce the times for recursion?

Is sorting the int array from high to low before recursion a better way? I dislike any recursive function which rely on external outside-the-method values. In your case, the sumInStack is external. This makes the target hard to 'see'. Additionally, if we do sort the data, there are some benefits we can have, and a way to restructure the recursion to make it do less work since we can guarantee that all values after a point have certain properties As for the 'unrolled' no recursion version of the system, it can be done.

It would require three int[] arrays:. The sum gives and indices act like a stack, and the depth is how deep the stack is again, assume sorted data :. Another way to do problems like this — investigating properties of all subsets that is, members of the "power set" — is to think of the main set as a list of cells, and each cell as a binary digit position. A member of the power set can therefore be described by a binary number, such that the subset contains only those elements of the set corresponding to a 1 in the binary value.

By doing that, you can generate the power set just by counting. Of course this gets a little complicated when the original set has more values in it that can be comfortably dealt with by the native integer type in a given programming language, but Java has BigInteger.

Enumerating a power set for any purpose is going to be a little painful for original sets that big anyway. I have not fully worked it out, but the best algorithm here is probably dynamic programming. Basically, I would order the values and at each one keep all possible sums, considering earlier sums.

Note that there is some efficiency above because some combinations are repeated many times. The further you go, the more such redundant values happen.

I have not worked out is if this would clearly be more efficient than using brute force search, but I am pretty sure it would. Dynamic programming should increase the memory requirement of the algorithm, but decrease the compute time. The example table I made would be useful to answer whether a given sum can be attained or not, but not to give all combinations that can produce a sum, if it exists. To answer that second question, the table would have to be modified to also associate with each output sum value all the combinations which can produce it.

Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Find all subsets of an int array whose sums equal a given target Ask Question. Asked 6 years, 4 months ago. Active 4 years, 6 months ago. Viewed 80k times.

For example: Target sum is Stack class to implement this function, along with recursion. GetAllSubsetByStack class import java. Is there a way to improve the code without using recursion?

Active Oldest Votes.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

combination of numbers that equal a given sum java

The sum indicates that it takes the sum of f x 1 ,x 2I have an idea for the solution, but it is a terribly ineffective algorithm using binary, and it's O 2 n. Of course, I cannot use "for" because it must be used for n non-specific times.

The way to solve this is to think of it as an attempt to find all combinations of n numbers between min and max. Adding them up as you go. You can imagine n stones on a number line between min and maxthen moving the furthest right stone until it is at max, when it is you move the next furthest right stone up one place and move all stones to its right.

Learn more. Calculate Sum of numbers with all possible combination [closed] Ask Question. Asked 6 years, 6 months ago. Active 6 years, 6 months ago. Viewed 2k times. Is there anyone who knows more effective algorithm for this? Denim Datta 3, 3 3 gold badges 24 24 silver badges 53 53 bronze badges.

StackExchange Math. StackExchange 1 1 silver badge 8 8 bronze badges. Could you explain a little more what this algorithm is supposed to do. Perhaps in sudo code. StackExchange Sep 30 '13 at Active Oldest Votes. Adding them up as you go You can imagine n stones on a number line between min and maxthen moving the furthest right stone until it is at max, when it is you move the next furthest right stone up one place and move all stones to its right.

Richard Tingle Richard Tingle Thank you. Your graph helped me a lot to get how I should design the program. That's far faster than mine, so I don't have any problem now.

combination of numbers that equal a given sum java

The Overflow Blog. Podcast Cryptocurrency-Based Life Forms. Q2 Community Roadmap. Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap.


thoughts on “Combination of numbers that equal a given sum java

Leave a Reply

Your email address will not be published. Required fields are marked *