educative.io

Educative

Better solution without division

var contiguousSubArray = function (input, target) {
    var result = [];
    var inputLength = input.length;
    for (var index = 0; index < inputLength; index += 1) {
        var startPointer = index;
        var endPointer = index;
        var product = input[startPointer];
        while (product < target) {
            result.push(input.slice(startPointer, endPointer + 1)); // + 1 to make it inclusive
            endPointer += 1;
            product *= input[endPointer];
        }
    }
    return result;
}

This would avoid division by issue.

Hi @Gaurav7

Thank you for the suggestion. It is great seeing our users thinking out of the box and coming up with a better solution. Your feedback is much appreciated!