Friday, November 25, 2016

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.
Constraints
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  and selecting  consequetive digits, we have  and . Where  gives maximum product as 
  • For  lies in all selection of  consequetive digits hence maximum product remains 0
NOTE: WRITE YOUR PROGRAM IN COMMENTS SECTION



**********************************
PROGRAM
*********************************

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int t; 
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        int n; 
        int k; 
        scanf("%d %d",&n,&k);
        char* num = (char *)malloc(512000 * sizeof(char));
        scanf("%s",num);
        long long int max=0,i,j,p;
        long long int prod;
        for(i=0;i<=(n-k);i++)
        {
              p=i,prod=1;  
              for(j=1;j<=k;j++,p++)
              {
                  int temp=num[p]-48;
                  prod=prod*temp;
              }
              if(prod>max)
                  max=prod;
              
        }
        printf("%lld\n",max);
    }
    return 0;
}

Contact Geek Placement Preparation

Name

Email *

Message *