0
votes

This is my first post so i will try and make it as clear and easy to understand as possible.

I would like some help writing a method that should fill an int [] (1D) with all combinations of numbers from one to a give value. The values can be overwritten every time. As an example. If the array was length 3, I would like the progression of the array to look something like this:

111, 211, 121, 112, 221, 122, 212, 222, 311, 113, 131, 321, 132, 231, ......

I thought the best way to break it down might be to forgot about order (so if the array had been 211, 112 would be unnecessary) and then have a separate method to permute the array and every step. So the method I am looking to create would only need to make the array evolve something like this:

111 211 221 222 311 321 322 332 333

I could then simply run the permute method in between each of these array states.

I managed to make a method that showed:

111 211 112 121 221 122 212 222 322 223 232 332 233 323 333 433...

This had all the permutations (which isn't essential for what I am now looking to do) but when I coded it I didn't make the numbers actually go back and cover every combinations (e.g.: 311) (the difference between the numbers in my arrays was only of one each time)

Some background information: I am making a program that balances chemical reactions by adding the correct stoichiometric coefficients in front of every compound. eg: _H20 + _02 = _H2O2

and _ represents a location for a coefficient. I have a functioning method that checks if the equation is balanced and I need to manage to try every combination of constants until the equation is balance (or until a limit is reached). So i decided to do this with an int[] and the fill the equation coefficients from the array.

Im willing to post more information or detail about my program if it interests anyone of if it may help answer my question.

Thanks, looking forward to here your answers/oppinions.

Have a good one!

1
Okay, and what have you tried so far? What specifically is the issue that you're encountering? If you have some code, you should post it.tnw
"I managed to make a method that showed..." Can we see the method?chancea
I reviewed my code and realized it did not even give every permutation. I can't post the code because It's really scrappy and written in the main method, it also depends on some other methods and classes, I tried to tidy it up so it is separated better into distinct methods each with their use, I think it will be better if I restart and post what I manage to code. I think I should also have give some background information for why I would like this array; I will edit my main post with this.Max

1 Answers

1
votes

I'm not sure if java has a next_permutation method but in c++ it's easy to do with this function, this is a guide where you can inplement with c++, i hope this could help you. This is the link: http://www.cplusplus.com/reference/algorithm/next_permutation/

But you can also read this blog: http://codeforces.com/blog/entry/3980 this explain very clear what you want to do.