# I need help solving this algorithm problem, arrays related

Okay have you code anything what did you tried so far?

You can sort the `candies` array and proceed forward.

I tried sliding window approach and it didn’t help. This problem has too many “conditions”.

``````function maxSubarraySum(arr, num){
let maxSum = 0;
let tempSum = 0;
if (arr.length < num) return null;
for (let i = 0; i < num; i++) {
maxSum += arr[i];
}
tempSum = maxSum;
for (let i = num; i < arr.length; i++) {
tempSum = tempSum - arr[i - num] + arr[i];
maxSum = Math.max(maxSum, tempSum);
}
return maxSum;
}

maxSubarraySum([33, 20, 12, 19, 29],3)
``````

What exactly do you mean?

You can sort the array [ascending order] and start adding the elements [from first index] till the threshold.

This code does not work

``````/*Your main priority is to eat the maximum number of candies possible,
but if there are multiple ways of doing this, choose the one with the
fewest grams of sugar total.If there's still a tie, choose the lower
indices*/

function sugarHigh(candies, threshold){
pairList = number_the_sweets(Candies,0,[]);
candies_Sorted_By_Sugar = lists.sort(PairList);
consumed = take_until_threshold(candies_Sorted_By_Sugar,Threshold,[]),
candyIndexes = [index || {_SugarGrams,Index} <- Consumed],
lists.sort(CandyIndexes)

};

function number_the_sweets([],_,Acc){
Acc;
number_the_sweets([SugarGrams|RestOfList],Index,Acc)
number_the_sweets(RestOfList,Index+1,[{SugarGrams,Index}|Acc]).

take_until_threshold(Tolerance , [OneTooMany || Okay]) when Tolerance < 0,
Okay;
take_until_threshold([],_,Eaten_the_lot) -> Eaten_the_lot;
take_until_threshold([Sweet|Remaining],RemainingTolerance,Eaten) >
{SugarGrams,_Index} = Sweet,
take_until_threshold(Remaining,RemainingTolerance-SugarGrams,[Sweet|Eaten])
};

let candies = [0 < candies.length < 105 || 0 < candies[i] < 100];
let threshold = [1 < threshold < 109];

//TEST DATA
console.log(test);
console.log([2,3] = sugarHigh([33, 20, 12, 19, 29],33));
console.log([] = sugarHigh([62, 67, 100],8)
);
console.log([0] = sugarHigh([16, 39, 67, 16, 38, 71],17),
);
console.log([0, 1, 2, 3, 4] = sugarHigh([16, 3, 14, 17, 11],99));``````

Where are you getting that from?

The Google doc’s plus some files he send me in DM’s

@ddanilovic is that code that @KittyKora posted what you have written? If so, what programming language are you using? The instructions say JavaScript, but that is not JavaScript

They needed the code in JS
Here’s the original link they gave me: