Project Euler #8: Largest product in a series

  • + 0 comments

    JavaScript solution:

    /////////////// ignore above this line ////////////////////
    
    function main() {
        var t = parseInt(readLine());
        for(var a0 = 0; a0 < t; a0++){
            var n_temp = readLine().split(' ');
            var n = parseInt(n_temp[0]);
            var k = parseInt(n_temp[1]);
            var num = readLine();
            // <-- SOlution start -->
            let arr = []
            for(i=0;i< n;i++){
              let nums = Array.from(num,(a,b) => {
                while(b>=i && b< i+k) { 
                // Mapping Array if Index of number is Higher or Equal to the current loop index and less than the sum of K and current loop index
                  return a
                  }
              }).filter((a) => a!= undefined ) // Exclude Undefined
              if(nums.length == k) arr.push(nums.reduce((a,b) => a*b,1)) // Push Multiplied results if the array's length equals to K
            }
            console.log(arr.sort((a,b) => a-b)[arr.length-1])
            // <-- SOlution End -->
        }
    }