From 4514afb011a0fed13fa7cbb997f8c3f7e16012f8 Mon Sep 17 00:00:00 2001 From: Raghavendra naik Date: Wed, 24 Oct 2018 12:48:26 +0530 Subject: [PATCH] Update CheatSheets/flexbox-cheatsheet --- CheatSheets/flexbox-cheatsheet | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/CheatSheets/flexbox-cheatsheet b/CheatSheets/flexbox-cheatsheet index 82bfafe..3e3eb40 100644 --- a/CheatSheets/flexbox-cheatsheet +++ b/CheatSheets/flexbox-cheatsheet @@ -1 +1,33 @@ -Create a descriptive well formatted cheatsheet for all the things you should know about Flexbox \ No newline at end of file +/** + * Given an array of positive numbers, find the maximum sum of a subsequence + * with the constraint that no 2 numbers in the sequence should be adjacent in the array. + * So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7). + * Answer the question in most efficient way. + * Input : arr[] = {5, 5, 10, 100, 10, 5} +Output : 110 +Input : arr[] = {1, 2, 3} +Output : 4 + */ + +function maxSumOfNumbers(var values, var j) { + var initVal = values[0]; + var curVal = 0; + var tempVal; + var k; + + for (k = 1; k < j; k++) + { + if(initVal > curVal) + tempVal = initVal; + else + tempVal = curVal; + + initVal = curVal + arr[k]; + curVal = tempVal; + } + + if(initVal > curVal) + return initVal; + else + return curVal; +} \ No newline at end of file