STRCH APRIL LONG

I tried to approach this problem by subtracting those substrings which do not have the required character from the total no of substrings.I am getting correct answers for every test case I can come up with but still getting WA for some test cases.Can anyone identify what I am doing wrong?
My code is provided here : STRCH

1 Like

It’s an overflow.
At line 6, you’re casting N(N+1) to long after the multiplication.
You gotta cast N first like (long)N * (N+1) / 2

3 Likes

@creep04
Can you please help me, I’m getting WA for some test cases, I tried many test cases manually I’m getting correct answer for them, can anyone tell me what is wrong with my code CodeChef: Practical coding for everyone

@anon79627731 can you please assist me with solution

  1.   long long int n,i,vikky=0,l=0;
    
    scanf("%lld",&n);
    //printf("%lld\n",n);
    char kiran[n],p;
    scanf("%s %c",kiran,&p);
    // printf("%s %c\n",kiran,p);
    for(i=0;i<n;i++){
    char pp=kiran[i];
    if(pp==p){
    l=i+1;
    }
    vikky=vikky+l;
    }
    printf("%lld\n",vikky);

why it assign i+1 to l and increcment vikky by l

  1. long su(long n) { return (n*(n+1))/2; }
    scanf("%ld",&n);
    char a[n];
    getchar();
    scanf("%s",a);
    char x;
    getchar();
    scanf("%c",&x);
    long z=su(n);
    long c=0;
    for(int i=0;i<n;i++)
    {
    if(a[i]!=x)
    c++;

     else
     {
     	z=z-su(c);
     	c=0;
     }
    

    }
    z=z-su©;
    printf("%ld\n",z);

it always minus the sum when char is not required and put again c=0
please specify the logic

any other logic is also appreciated