settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Arithmephile - JavaScript task

+3 votes

You will be given an input of an arbitrary amount of numbers. If a number S is a digit (0 >= S < 10), calculate the product of multiplication of the next S numbers. Find the biggest product among all the S intervals. Note that the intervals may overlap – when you’ve encountered a number that fits the requirement and have calculated the product, the next valid number S may be within this interval.

Input

  • The input data is passed to the first JavaScript function found in your code as an array of strings that need to be parsed as numbers.

Output

  • A number, the biggest multiplication should be printed on the console.

Constraints

  • The input may contain up to 10,000 lines (elements)
  • The numbers in the input are in range [-1..10,000] inclusive
  • The numbers denoting ranges (S) are in range [0..9] inclusive
  • Allowed time/memory: 100ms/16MB

Examples:

Input:
10
20
2
30
44
3
56
20
24


Output:
26880


Input:
100
200
2
3
2
3
2
1
1

Output:
12

asked in JavaScript category by user icabe

1 Answer

+2 votes

Here is the solution:

function arithmephile(input) {
    let numbers = input.map(Number);
    let biggestProduct = Number.MIN_SAFE_INTEGER;
    for (let i = 0; i < numbers.length; i++) {
        let currentNumber = numbers[i];
        if (currentNumber >= 0 && currentNumber < 10) {
            let currentProduct = 1;
            for (let j = i + 1; j <= i + currentNumber; j++) {
                let currentMultiplier = input[j];
                currentProduct *= currentMultiplier;
            }
            if (currentProduct > biggestProduct) {
                biggestProduct = currentProduct;
            }
        }
    }
    console.log(biggestProduct);
}

//arithmephile([
//    "10",
//    "20",
//    "2",
//    "30",
//    "44",
//    "3",
//    "56",
//    "20",
//    "24"
//]);

arithmephile([
    "100",
    "200",
    "2",
    "3",
    "2",
    "3",
    "2",
    "1",
    "1"
]);

 

answered by user john7
edited by user golearnweb
...