maintain 2 sums : incl and xclud. incl is the sum of the elements including the previous element. xclud is the sum of elements excluding the previous element.
now .. for evry element 1 to n-1. set xclud = max(incl, xclud) set incl = xclud (value from previous iteration) + a[i]. finally, print whicheva is the max. On Tue, Jul 19, 2011 at 5:01 PM, Nitish Garg <[email protected]>wrote: > Typo in my above post. > s[i] = max(s[i-2], s[i-2]+a[i], s[i-1]) work? > > > On Tue, Jul 19, 2011 at 4:59 PM, Nitish Garg <[email protected]>wrote: > >> Won't this recurrence work: >> s[i] = max(s[i-2], s[i-2]+a[i], a[i-1]) work? >> I think it works. >> >> >> On Tue, Jul 19, 2011 at 4:54 PM, oppilas . <[email protected]>wrote: >> >>> New constraint:- >>> What if the array also contains positive and negative numbers? >>> >>> On Tue, Jul 19, 2011 at 4:36 PM, sagar pareek <[email protected]>wrote: >>> >>>> ok ok ok thank you all >>>> >>>> >>>> On Tue, Jul 19, 2011 at 4:35 PM, ankit sambyal >>>> <[email protected]>wrote: >>>> >>>>> @Sagar: 13 is the correct answer. (6+4+3) >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Algorithm Geeks" group. >>>>> To post to this group, send email to [email protected]. >>>>> To unsubscribe from this group, send email to >>>>> [email protected]. >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/algogeeks?hl=en. >>>>> >>>>> >>>> >>>> >>>> -- >>>> Regards >>>> SAGAR PAREEK >>>> COMPUTER SCIENCE AND ENGINEERING >>>> NIT ALLAHABAD >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Algorithm Geeks" group. >>>> To post to this group, send email to [email protected]. >>>> To unsubscribe from this group, send email to >>>> [email protected]. >>>> For more options, visit this group at >>>> http://groups.google.com/group/algogeeks?hl=en. >>>> >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Shubham Maheshwari ShubZz O.o o.O enJoY ...!!! -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
