Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

maximum sum of non-adjacent elements

public static void NumbersSumb(int[] arr)
{
    if(arr.Length <=2)
      	Console.WriteLine(arr.Max());

    arr[2] = arr[0] + Math.Max(0, arr[2]);

    for (int i = 3; i < arr.Length; i++)
      	arr[i] = Math.Max(arr[i - 2], arr[i - 3]) + Math.Max(0, arr[i]);

    Console.WriteLine(Math.Max(arr[arr.Length - 1], arr[arr.Length - 2]));
}
 
PREVIOUS NEXT
Tagged: #maximum #sum #elements
ADD COMMENT
Topic
Name
3+2 =