Given elements in a array find combinations that equal destination value

狂风中的少年 提交于 2019-12-23 18:51:39

问题


I have been pondering over this problem quite some time and for some reason it is not getting through my head. If i am given an array [1,2,3,4] and a destination value of say 5. I want to get all possible combinations from the array such that they add to the destination value.

So one approach i can think of is

1!=5
1+2!=5
1+3!=5
1+4=5
//now check with combos of 3 digits.
1+2+3 !=5
1+2+4 !=5
//now check with combos of 4...however at this point i realized i missed the combination 1+3+4 also.

I have seen a few answers online but they do not seem to make sense and i am not really interested in others answers or codes, i want to learn how to think the correct way so i can solve such problems. i was wondering if someone can guide me and help me break down this problem and also how i should be thinking to solve such problems. I am not really worried by efficiency at this point because i have not even been able to formulate an approach so all approaches are welcome. Also i prefer using just array and normal loops not any other data structures like hash as i have not learned those yet.


回答1:


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...



来源:https://stackoverflow.com/questions/25811373/given-elements-in-a-array-find-combinations-that-equal-destination-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!