32 Answers
New AnswerI 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? https://code.sololearn.com/cHLw7Lm7L83A/?ref=app
1/11/2021 8:59:57 PM
Esraa Hesham32 Answers
New AnswerHere 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,b,c,d,e,f,g,...] a -> ac -> ace .. g -> acf -> ad -> adf -> adg - 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 https://code.sololearn.com/caZ8lhi2WifY/?ref=app
In the name of kindly generous GOD Esraa Hesham excuse me Can you explain more ,that your function what do it do?
https://code.sololearn.com/cu1zGlsoIUSA/?ref=app I think you sum is also wrong I changed a little bit but it didn't helped
اَلْحَمْدُ لِلّٰهِ Thanks ALLAH I solved by python & cpp i tried to make a way or algorithm that is very easy work! https://code.sololearn.com/cNO0X1MpzKTY/?ref=app give nums in first input line give sum on the next line such as : 1 2 3 4 5 output(indices): [0,3] (0 -> 1 + 3 -> 4)(1+4==5) It solved in c# too :)) Code c#: https://code.sololearn.com/cA23A118a18A/?ref=app
Abol Sure!! Given an array of N, each element of the array represents the gain. I should find the maximum amount of gain. No two elements are adjacents.
Thank you Stefanoo The function works now but the result should be 9 not 11. And the Indices of the element should be in the output. 5+3+1
Stefanoo I appreciate your help! I have been trying to fix it the last few days but I couldn't. I don't know how to solve this!
Learn Playing. Play Learning
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message