howSum(7, [5, 3, 4]) -> [4, 3]
m = targetSum
n = numbers.length
first create an array the size of the target sum + 1
set default value to be null
index: 0 1 2 3 4 5 6 7
value: null null null null null null null null
when targetSum is at 0, no sum is required to get 0, therefore return value should be []
index: 0 1 2 3 4 5 6 7
value: [] null null null null null null null
look at the 1st element of the array [5, 3, 4] is 5
current index is 0 and value is []
current index 0, can return 0 by not adding
at 5 steps ahead of the current index,
value can be changed to the same as current value [] and appends the current element 5 into it
resulting to [5]
index: 0 1 2 3 4 5 6 7
value: [] null null null null [5] null null
look at the 2nd element of the array [5, 3, 4] is 3
current index is 0 and value is []
current index 0, can return 0 by not adding
at 3 steps ahead of the current index,
value can be copied from the current value [] and append the current element 3 into it
resulting to [3]
index: 0 1 2 3 4 5 6 7
value: [] null null [3] null [5] null null
look at the 3rd element of the array [5, 3, 4] is 4
current index is 0 and value is []
current index 0, can return 0 by not adding
at 4 steps ahead of the current index,
value can be copied from the current value [] and append the current element 4 into it
resulting to [4]
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
move current value to the next index
look at the 1st element of the array [5, 3, 4] is 5
current index is 1 and value is null
if current value is null, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
look at the 1st element of the array [5, 3, 4] is 3
current index is 1 and value is null
if current value is null, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
look at the 1st element of the array [5, 3, 4] is 4
current index is 1 and value is null
if current value is null, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
move current value to the next index
look at the 1st element of the array [5, 3, 4] is 5
current index is 2 and value is null
if current value is null, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
look at the 1st element of the array [5, 3, 4] is 3
current index is 2 and value is null
if current value is null, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
look at the 1st element of the array [5, 3, 4] is 4
current index is 2 and value is null
if current value is null, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
move current value to the next index
look at the 1st element of the array [5, 3, 4] is 5
current index is 3 and value is [3]
at 5 steps ahead of the current index,
it is out of range, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] null null
look at the 2nd element of the array [5, 3, 4] is 3
current index is 3 and value is [3]
at 3 steps ahead of the current index,
value can be copied from the current value [3] and append the current element 3 into it
resulting to [3, 3]
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] [3, 3] null
look at the 3rd element of the array [5, 3, 4] is 4
current index is 3 and value is [3]
at 4 steps ahead of the current index,
value can be copied from the current value [3] and append the current element 4 into it
resulting to [3, 4]
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] [3, 3] [3, 4]
we can stop here, but just for example purposes, we will continue 1 more step
we can continue the loop till the end and it would still provide the same valid answer
move current value to the next index
look at the 1st element of the array [5, 3, 4] is 5
current index is 4 and value is [4]
at 5 steps ahead of the current index,
it is out of range, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] [3, 3] [3, 4]
look at the 2nd element of the array [5, 3, 4] is 3
current index is 4 and value is [4]
at 3 steps ahead of the current index,
value can be copied from the current value [4] and append the current element 3 into it
resulting to [4, 3]
note that value at index 7 gets overwritten
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] [3, 3] [4, 3]
look at the 3rd element of the array [5, 3, 4] is 4
current index is 4 and value is [4]
at 4 steps ahead of the current index,
it is out of range, nothing needs to be changed
index: 0 1 2 3 4 5 6 7
value: [] null null [3] [4] [5] [3, 3] [4, 3]