HACKER RANK Project Euler 08 - Largest Product in a Series - Solution.'C++'

Project Euler Challenges-08 - Largest Product in a Series Solution
Problem Statement
This problem is a programming version of Problem 8 from projecteuler.net
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

Solution:

    #include <iostream>
    #include <string>
    using namespace std;
    string s;
    int getMult(int left, int right)
    {
    int ans=1;
    for(int i=left;i<=right;++i)
    ans=ans* ( s[i]-'0');
    return ans;
    }
    int main()
    {
    int t;
    cin>>t;
    for(int k=1;k<=t;++k)
    {
    int ans=0,n,m;
    cin>>n>>m;
    cin>>s;
    for(int i=0;i<s.size()-m;++i)
    ans=max(ans, getMult(i,i+m-1));
    cout<<ans<<endl;
    }
    return 0;
    }

Thanks for Visiting, Hope this helps you....

Comments

Popular Posts