Project Euler #8: Largest product in a series

Sort by

recency

|

210 Discussions

|

  • + 0 comments

    Okay. I'm improving. This should have taken me atleast an hour. Done it in 20 mins. C# solution

    private static int getProduct(int[] arr)
        {
            int product = 1;
            foreach(int x in arr)
                product *= x;
            return product;
        }
        
        public static void printAnswer(string num, int n, int k)
        {
            int highestProduct = 0;
            int c = n-k+1; //max combinations
            for(int i = 0; i < c; i++)
            {
                string newStr = num.Substring(i,k);
                int [] arr = newStr.Select(x => x - '0').ToArray();
                int product = getProduct(arr);
                if(product > highestProduct)
                {
                    highestProduct = product;
                }
            }
            Console.WriteLine(highestProduct);
        }
    
  • + 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 -->
        }
    }
    
  • + 0 comments

    All test cases passed:

    def Solve_Prob(n,k,strm): maxi = -1 for i in range(0,n-k+1): pro = 1 for j in range(i,i+k): pro = pro*int(strm[j]) if pro > maxi: maxi = pro answer = maxi return answer

    t = int(input()) for i in range(0,t): n,k = list(map(int,input().split())) m = input() answer = Solve_Prob(n,k,m) print(answer)

  • + 0 comments

    Javascript

    function greatestProduct (n,k,num) {
        let product = 0;
      arrayOfEl = num.split("");
    
      for (let i = 0; i <= n - k; i++) {
        const multiplication = arrayOfEl
          .slice(i, k + i)
          .reduce((acc, curr) => (acc *= curr));
        if (multiplication > product) product = multiplication;
      }
      console.log(product);
    }
    
    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();
            greatestProduct(n,k,num)
        }
    
  • + 0 comments

    Can someone find whats wrong with my code?

    #!/bin/python3
    
    import sys
    
    
    t = int(input().strip())
    for a0 in range(t):
        n,k = input().strip().split(' ')
        n,k = [int(n),int(k)]
        num = input().strip()
        # prefix array
        marr = [int(num[0])]
        for i in range(1,len(num)):
            marr.append(marr[-1](int(num[i])))
        #print(marr)
        res = 0
        #find max prodcut
        for i in range(k,len(marr)):
            if marr[i-k] !=0:
                prod = marr[i]//marr[i-k]
            res=max(prod , res )
        res = max(res,marr[k-1])
        print(res)
    

    *