Wrong Solution

Problem Code - CV
Link - https://www.codechef.com/problems/CV

The Example Input fails to give correct output , ( output for ‘abu’ gives 2 instead of 1 ).
What’s wrong with the code , PLS HELP.


#include<bits/stdc++.h>
int main()
{
    int t;
    std::cin>>t;
    for(int i=0;i<t;i++)
    {
        int vowels[] = {97,101,105,111,117};
        int n_letters , count=0;
        std::cin>>n_letters;
        char letters[n_letters];
        for(int j=0;j<n_letters;j++)
        {
            std::cin>>letters[j];
        }
        for(int j=0;j<n_letters;j++)
        {
            for(int k=0;k<(sizeof(vowels)/sizeof(int));k++)
            {
                if((int(letters[j])!=vowels[k]) && letters[j+1]==vowels[k])
                {
                    count++;
                }
            }
        }
       std::cout<<"\n"<<count<<"\n";
    }
    std::cout<<"\n";
    return 0;
}

Test Case :
1
2
ea

Your output :
1

Correct output :
0

1 Like

You have a lot of logical errors in your code. This line
(int(letters[j])!=vowels[k]) && letters[j+1]==vowels[k]

is incorrect, for example if the current letters index has u and the next one is a, and the current vowels index has a too, it’ll increase count by 1 which is probably not what you want.
The code below may be C-style 'cause I DON’T like C++.

#include<bits/stdc++.h>
int vowels[] = {97,101,105,111,117};
int ifvowel(char s){
    for(int i=0;i<5;i++){
        if(vowels[i]==s){
            return 1;
        }
    }
    return 0;
}
int main()
{
    int t;
    int count=0;
    std::cin>>t;
    for(int i=0;i<t;i++)
    {
        int n_letters;
        std::cin>>n_letters;
        char letters[n_letters];
        for(int j=0;j<n_letters;j++)
        {
            std::cin>>letters[j];
        }
        for(int j=0;j<n_letters;j++)
        {
            if(!ifvowel(letters[j]) && ifvowel(letters[j+1]))
            {
                count++;
            }
        }
        std::cout<<count<<"\n";
        count=0;
    }
    return 0;
}
1 Like

Oh! got it , understood mistake in my code now…
Thank you so much :smiley:!