Wrong Answer Debugging

Hello,
I’ve been getting wrong answer on these two problems : turbo sort and holes in the text. They run just fine on my compiler - I’m using xcode.
Kindly help.

//Holes in the text

int main()
{
int t, i, j = 1;
cin >> t;
if (t > 40)
return 1;
char hole[100];
int count[40];

while (j <= t)
{
    scanf ("%s", hole);
    count[j] = 0;
    for (i = 0; i < 40; i++)
    {
        if (hole[i] == 'A' || hole[i] == 'D' || hole[i] == 'O' || hole[i] == 'P' || hole[i] == 'R' || hole[i] == 'Q')
            count[j]++;
        else if (hole[i] == 'B')
            count[j] += 2;
        
    }
    cout << count[j] << endl;
    j++;
}

/*for (j = 1; j <= t; j++)
 cout << count[j] << endl;*/

return 0;

}

//Turbo

long turbo[1000000] = {0};

int main()
{
long j, x = 0, i, p;
long maxx = 0;
cin >> j;

for (i = 0; i < j; i++)
{
    scanf("%ld", &x);
    if (x < 1000000)
    {
        turbo[x] = x;
        if (x > maxx)
        maxx = x;
    }
}
    
for (p = 0; p <= maxx; p++)
{
    if (turbo[p] > 0)
        printf ("%ld \n", turbo[p]);
}

return 0;

}

Regarding the turbo sort Question…
Your logic has some error…Currently you are just checking whether a number has occurred or not BUT

  1. You must count the number of occurrences of each number.

  2. Then print each and every occurrence.

  3. You must also keep in mind that number can be less than or equal to 106. So the array should be of size 1000001, in order to have index 1000000.

The edited code is as follows:

    #include 
    #include 
    using namespace std;
    long turbo[1000001] = {0};
    int main()
    {
       long j, x = 0, i, p;
       long maxx = 0;
       cin >> j;
       for (i = 0; i < j; i++)
       {
          scanf("%ld", &x);
          if (x <= 1000000)
          {
            turbo[x]++;
            if (x > maxx)
            maxx = x;
          }
       }
      for (p = 0; p <= maxx; p++)
      {
        while(turbo[p]--)
            printf ("%ld\n",p);
      }
      return 0;
    }   

1 Like

For holes in the text, as j can be up to 40 (j <= t), and you are accessing count[j], size of count should be 41.

i.e. int count[41]; instead of int count[40];

Regarding the Holes Question:

  1. You made the same mistake of array index…Here the number of test cases are <=40 so you array size should be atleast 41 to accomodate index 40 in it.

  2. The length of the string can be anything less than 100. So running loop up to 100 always will lead to Wrong Answer, as there can be garbage after the actual length of the string…to keep a check for that you must run the loop only up to the length of the string.

  3. You must always keep the size of a char array 1 more than the max size of the string , so that ‘\0’ can also be accommodated in the array as it is the check for the length of the string.

The edited code is as follows:

    #include 
    #include
    #include 
    using namespace std;
    int main()
    {
       int t, i, j = 1;
       cin >> t;
       char hole[101];
       int count[41];
       while (j <= t)
       {
          scanf ("%s", hole);
          count[j] = 0;
          int len=strlen(hole);
          for (i = 0; i < len; i++)
          {
            if (hole[i] == 'A' || hole[i] == 'D' || hole[i] == 'O' || hole[i] == 'P' || hole[i] == 'R' || hole[i] == 'Q')
                count[j]++;
            else if (hole[i] == 'B')
                count[j] += 2;
          }
          cout << count[j] << endl;
          j++;
       }
       /*for (j = 1; j <= t; j++)
        cout << count[j] << endl;*/
       return 0;
    }   
1 Like

Hello! Thank you for pointing out the array length error. I guess that was giving me the SIGSEGV error.