'Recursion for finding the smallest sum of numbers in an array
I have a problem.
I need to sum up the arrays values to get the lowest possible sum.
Rules:
You go to next step and take the value (step 1->2)
You skip 2 and take double the value (step 1->4, skipping the 2nd and 3rd elements)
in an array of values 1 5 1 60 1 5 3
for this array the answers would be 20 but the code gives the result 30
So far I have partial solution:
static int recur(int [] arr, int index, int sum)
{
if (index >= arr.Length) return 1000;
sum += arr[index];
if (index == arr.Length - 1)
{
return sum;
}
else
{
return Math.Min(recur(arr, index + 1, sum), 2* recur(arr, index + 3, sum));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
