Zig Zag Sequence

  • + 0 comments

    it is broken in js :(

       const [_, length, rawArr] = input.split('\n');
    
    const arr = rawArr.split(' ').sort((a, b) => a - b);
    
    
    let mid = Math.floor(length / 2);
    
    [arr[mid], arr[length - 1]] = [arr[length - 1], arr[mid]]
    
    let left = mid + 1;
    let right = length - 2;
    while (left < right) {
        [arr[left], arr[right]] = [arr[right], arr[left]];
        left++;
        right--;
    }
    
    const str = arr.join(' ');
    const strTrim = str.trim();
    
    
    console.log(strTrim);