javascriptarraysalgorithmkadanes-algorithm

find all subarrays in o(n) time 1D array using javascript


I want to collect all subarrays for further computation efficiently in javascript. I'm not sure this is possible, but it seems for a subarray sum kadane's formula is o(n) which is more efficient than other methods. But I'm not sure I how I can store the array at each step.

Similar to this quora question, for me the pseudo code was not enough. Thanks for the further breakdown.

another meta link

an example in action of this for [3, 3, 9, 9, 5]

[3], [9], [5], [9, 5], [9, 3], [9, 9], [3, 3],
[3, 9, 9], [3, 3, 9], [9, 9, 5], [3, 3, 9, 9],
[3, 9, 9, 5], [3, 3, 9, 9, 5]

Solution

  • This is fairly simple to do: https://jsfiddle.net/j1LuvxLq/

    All you do is iterate possible lenghts and starting points and just print out the subsets. Complexity is O(n²) where n is the length of the original array. No way to improve it thought because that's the order of how many subsets there are.

    var set = [3, 3, 9, 9, 5].join('')
    
    var set_length = set.length
    
    var subsets = []
    
    for (var length_of_subset = 1; length_of_subset <= set_length; length_of_subset++) {
        for (var start_of_subset = 0; start_of_subset <= set_length - length_of_subset; start_of_subset++) {
            var current_subset = set.substring(start_of_subset, start_of_subset + length_of_subset)
            if(subsets.indexOf(current_subset) == -1) {
                subsets.push(current_subset.split(''))
            }
        }
    }
    
    // print the subsets out
    for (s in subsets) {
        $('body').append(subsets[s].join(', ') + '<br>')
    }
    

    Alternative, possibly nicer solution would be to use dynamic programming. Start with 3 and either remove last element or add next element. Check it out here: https://jsfiddle.net/p82fcs4m/

    var set = [3, 3, 9, 9, 5].join('')
    var subsets = []
    
    take(set[0], set.substring(1))
    
    function take(chosen, left) {
    
        if(subsets.indexOf(chosen) != -1) {
            return
        }
    
        subsets.push(chosen)
    
        if (chosen.length > 1) {
            take(chosen.substring(1), left)
        }
    
        if (left.length > 0) {
            take(chosen.concat(left[0]), left.substring(1))
        }
    }
    
    $('body').append(subsets.join('<br>'))