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
Find the greatest product of K consecutive digits in the N digit number.
Input Format
First line contains T that denotes the number of test cases.
First line of each test case will contain two integers N & K.
Second line of each test case will contain a N digit integer. 
Output Format
Print the required answer for each test case.
Constraints
1T100
1K7
KN1000
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))
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
0

Add a comment

Popular Posts
Popular Posts
print ("About Me")
print ("About Me")
My Photo
HYDERABAD, Telangana, India
Blog.History(All)
Labels
Total Pageviews
Total Pageviews
21466
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.