Project Euler #8: Largest product in a series | Hackerrank | Project Euler

 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.

Constraints

1<=T<=100
1<=K<=7
K<=N<=1000

Output Format

Print the required answer for each test case.

Sample Input 0

2
10 5
3675356291
10 5
2709360626

Sample Output 0

3150
0

Explanation 0

For 3675356291 and selecting K=5 consecutive digits, we have 36753, 67535, 75356, 53562, 35629 and 56291. Where 6*7*5*3*5 gives  maximum product as 3150
For 2709360626, 0 lies in all selection of 5 consecutive digits hence maximum product remains 0

Solution:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int t;
    cin >> t;
    for(int a0 = 0; a0 < t; a0++){
        int n;
        int k;
        cin >> n >> k;
        string num;
        cin >> num;
    int j=k;
    int l=0;
    int max=0;
    while(j<=n)
    {
        int mul=1;
        for(int i=l;i<j;i++)
        {
            mul=(int)(num[i]-48)*mul;
        }
    
        j++;
        l++;
        
        if(mul>max)
        max=mul;
    }
     cout<<max<<endl;
    }
    return 0;
}



You can also run it on an online IDE :

https://ide.geeksforgeeks.org/1a3d68dc-6e98-408d-9c36-f0dd139e8f4d

Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!

No comments:

Post a Comment

Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...