Hey guys,
Hope you all are doing great. I am back with another problem of Project Euler on
Project Euler #8: Largest product in a series
Hope you all are doing great. I am back with another problem of Project Euler on
Project Euler #8: Largest product in a series
Find the greatest product of consecutive digits in the digit number.
Input Format
First line contains that denotes the number of test cases.
First line of each test case will contain two integers & .
Second line of each test case will contain a digit integer.
First line contains that denotes the number of test cases.
First line of each test case will contain two integers & .
Second line of each test case will contain a digit integer.
Output Format
Print the required answer for each test case.
Print the required answer for each test case.
Constraints
Sample Input
2
10 5
3675356291
10 5
2709360626
Sample Output
3150
0
Algorithm:
As easy can it get, this is a simple problem to directly multiply the sequence of number.
Just we need to take care of the thing as the numbers can be very large, so handling them as character sequence is preferable.
Now its the trick how you convert it into a code. As we already know in Python we have a reduce function
Ex:
reduce(sum, range(5))
Just we need to take care of the thing as the numbers can be very large, so handling them as character sequence is preferable.
Now its the trick how you convert it into a code. As we already know in Python we have a reduce function
Ex:
reduce(sum, range(5))
Gives an output as 10, as sum is a operator(predefined function in python) which sums up the numbers in range 0 to 4
Similarly we can use mul from operators in python to reduce a subsequnce of k digits.
In my below code i have used a simple concept of product, but you can try the above approach to solve it as it worked for me.
And Presto!! Pythoned!!
Program:
from operator import mul t=input() while(t>0): n,m = map(int,raw_input().split()) num=list(raw_input()) ans=0 for i in range(0,n-m): x=1 for k in range(0,m): x=x*int(num[i+k]) ans=max(ans,x) print ans
Add a comment