I was trying to find the Maximum sum such that no two elements are adjacents, also the Indices of the chosen elements. But the output is not correct, can you solve the problem?
Here you go. Recursive was easier but definitly horrid runtime. Reminds me of Fibonacci so probably closer to 2^n.
Rules that can be assumed that give recursive form.
-either we take the first or second element
-if we take one then we do not take the following element
-we must take one of the two that follows
For sure will run in O(n) in for loop version
Seems to work. You'll have to add index stuff. Your logic is off on your inclusive exclusive.
Gave 20 instead of 18 on first case u gave.
Pretty much optimization is if you write these out, you see you get duplicates based on sums so far.
a -> ac -> ace .. g
-> ad -> adf
- assume a > b
b -> bd < ad
-> be < ace
*Would have posted last night but got something went wrong error.
Positive values only :)
So code: I got rid of long and made it ints
I solved by python & cpp
i tried to make a way or algorithm that is very easy work!
give nums in first input line
give sum on the next line
such as :
1 2 3 4
(0 -> 1 + 3 -> 4)(1+4==5)
It solved in c# too :))