LeetCode – Beautiful Array

Link: https://leetcode.com/problems/beautiful-array/

/**
 * @param {number} N
 * @return {number[]}
 */

var split = function (arr)
{
    if (arr.length <= 2)
        return arr
;

    var odd = [];
    var even = [];
    
    for (var i = 0; i < arr.length; i++)
    {
        if (i % 2)
            even.push(arr[i])
        else
            odd.push(arr[i])
    }
    
    return split(odd).concat(split(even));
}

var beautifulArray = function(N)
{
    return split(Array.from({length: N}, (x, i) => i + 1));
};

Leave a Reply

Your email address will not be published. Required fields are marked *