mission-peace / interview

Interview questions
Apache License 2.0
11.08k stars 5.17k forks source link

Update MaxSumForNonAdjacentElements.java #285

Open priya-hubcode opened 4 years ago

priya-hubcode commented 4 years ago
  1. Added corner cases based on the length of the array.
  2. arr[0] needs to be max(0, arr[0]). The assumption that the empty set is a valid subset comes into play. Were we to only consider subsets of size >= 1, we'd have to concern ourselves with finding the least negative number in an array of purely negative numbers. However, since we can use the empty set, 0 is a valid sum for an array of purely negative integers.