Since you said you want 'ways of thinking', this is one way.
You start with simplest of cases by making various assumptions
1) Assume all your array elements are smaller than the destination value. -- a simple validation will help when implementation is done.
High level steps
You need to find out a way of obtaining all possible permutations of the number.
And then add each element of the permutation and check if the sum matches 'destination' (This is simpler task)
now coming to the major challenge here:
how to obtain all possible permutations?
Solution Approach
Lets assume we have an array of single element:
SOlution is obvious :)
Next we have an array of two elements:
You determine the size of the array: 2
You have to iterate over this size because your combinations will be less than or equal to this size.
Your first iteration will have value 1. ie. you are looking for combinations of size one
This is simple: you iterate over the array one by one.
Next iteration will look for combinations of size 2.
Since the iteration value (2) is equal to size of the array (2), you know there is just one possible case.
Now lets handle an array of size 3:
for size 1 permutations, you know what to do.
for size 3 permutations, you know what the combination is.
for size 2 permutations, you require another iteration.
you iterate over the array, hold the current element of the array and permutate rest of the array of size 2.
Next you iterate over second and then third element, and permutate rest of the array of size (3-1 = 2)
--------------------------------UPDATED--------------------------------------------------
Next lets try an array having 4 elements
Lets call is A(4)
For size 1 permutations and size 4 permutations, its obvious.
For size 3 permutations, you will repeat process of what you did for obtaining size 2 permutations for an array of size 3.
You will hold one element, you will be left with an array of size 3. Let call it A(3)
But remember, you also need permutations of size 2. You can determine all permutations of size 2 from this array of size 3 itself by applying the same reusable component. This becomes a recursive call.
So essentially, you have to do one thing most of the time.
'If you have an Array of size n; A(n), you need to iterate through it, hold element being iterated and you will have an array of size n-1; A(n-1)'
then make a recursive call again.
and replace n with n-1 in the bold line
So essentially as you see this is turning into a recursive problem.
This is one way of thinking a solution of this problem. I hope I was clear in explaining the thinking process.
------------------------------------------Example------------------------------------------
Assume we have an array {1,2,3,4,5}
and our function looks like
function G (int[] array ) {
Looping over array{
remove array[index] from array
you are left with restOfTheArray .// store it some where.
then
make a recursive call to G with restOfTheArray;
}
}
Dry run for the loop:
Dry run 1:
funtion G (Array (n ) ) {
Looping over {1,2,3,4,5}{
first value while looping is 1, remove it from the array;
you have {2,3,4,5}.// store it some where.
then
make a recursive call to G with {2,3,4,5}
}
}
Dry run 2:
funtion G (Array (n ) ) {
Looping over {1,2,3,4,5}{
second value while looping is 2, remove it from the array;
you have {1,3,4,5}.// store it some where.
then
make a recursive call to G with {1,3,4,5}
}
}
and so on...
now lets look at the dry run of the recursive call:
Dry Run 1.1
funtion G (Array (n ) ) {
Looping over {1,2,3,4,5}{
First value while looping is 1, remove it from the array;
you have {2,3,4,5}.// store it some where.
then
make a recursive call to G with {2,3,4,5}
}
}
Dry Run 1.2
funtion G (Array (n ) ) {
Looping over {2,3,4,5}{
First value while looping is 2, remove it from the array;
you have {3,4,5}.// store it some where.
then
make a recursive call to G with {3,4,5}
}
}
and so on...