(solved) Can you help me to fix this error? | SoloLearn: Learn to code for FREE!

+4

(solved) Can you help me to fix this error?

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? https://code.sololearn.com/cHLw7Lm7L83A/?ref=app

1/11/2021 8:59:57 PM

Esraa Hesham

32 Answers

New Answer

+1

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

+4

This task is more difficult than it looks like. I think 1 for loop is not enough.

+2

In the name of kindly generous GOD Esraa Hesham excuse me Can you explain more ,that your function what do it do?

+2

Give an example!ple...

+2

What is your code output for this? (output of your code) [1,2,-4,24,25] gain=27

+2

What is your output ? (Indices)

+2

https://code.sololearn.com/cu1zGlsoIUSA/?ref=app I think you sum is also wrong I changed a little bit but it didn't helped

+2

الحمد لله The c# code solved Go and look at that!

+2

اَلْحَمْدُ لِلّٰهِ 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

+2

Thanks ALLAH Esraa Hesham

+2

Solved some bugs too

+1

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.

+1

Abol Sure. N = 6 V=[3, 2,5,6,6,9] Max gain=18 Indices [1,4,6] one based

+1

Max gained?what do you do?

+1

I undrestood by ALLAH helps

+1

Well where is wrong in your code?or error

+1

The expected output is 2,5 But it gives more Indices.

+1

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

+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!

+1

The algorithm must be less than O(N^2) So that what I thought of.