1

The problem was in week 6 of algorithmic toolbox course on Coursera. The problem is to find out the maximum value of an arithmetic expression consisting of + , - and * only.

I coded a solution, have run it with test cases and also incurred a stress test with other solutions available online. Everywhere my code seems to run fine. But whenever I am trying to submit it is failing the 5th test case. First I thought it was due to value overflow in long long, so submitted the solution with double data type. Still the problem remains.

The maximum and minimum functions

long long maximum(long long a,long long b,long long c,long long d)
{
    int a1 = a>b?a:b;
    int a2 = c>d?c:d;
    return a1>a2?a1:a2;
}

long long minimum(long long a,long long b,long long c,long long d)
{
    int a1 = a<b?a:b;
    int a2 = c<d?c:d;
    return a1<a2?a1:a2;
}

The eval function

long long eval(long long a, long long b, char op) {
  if (op == '*') {
    return a * b;
  } else if (op == '+') {
    return a + b;
  } else if (op == '-') {
    return a - b;
  } else {
    assert(0);
  }
}

The algorithm implementation

long long get_maximum_value(char *str) {
  int len = strlen(str);
  int n = (len+1)/2,i,j,k;
  long long a,b,c,d,a1,b1;
  long long **max = (long long **)malloc(n*sizeof(long long*));
  long long **min = (long long **)malloc(n*sizeof(long long*));
  for(i=0;i<n;i++)
  {
      max[i] = (long long*)malloc(n*sizeof(long long));
      min[i] = (long long*)malloc(n*sizeof(long long));
  }
  for(i=0;i<n;i++){
    max[i][i] = str[i*2] - '0';
    min[i][i] = str[i*2] - '0';
  }
  for(i=1;i<n;i++)
    for(j=0;i+j<n;j++)
    {
        max[j][i+j] = LLONG_MIN;
        min[j][i+j] = LLONG_MAX;
        for(k=j;k<i+j;k++)
        {
            a = eval(max[j][k],max[k+1][i+j],str[2*k+1]);
            b = eval(max[j][k],min[k+1][i+j],str[2*k+1]);
            c = eval(min[j][k],max[k+1][i+j],str[2*k+1]);
            d = eval(min[j][k],min[k+1][i+j],str[2*k+1]);
            a1 = maximum(a,b,c,d);
            b1 = minimum(a,b,c,d);
            if(a1>max[j][i+j]) max[j][i+j] = a1;
            if(b1<min[j][i+j]) min[j][i+j] = b1;
        }
    }
  return max[0][n-1];
}

Have anyone faced a similar problem? Please if possible suggest a test case for with the code will fail. It will be very helpful. Thanks in advance.

  • 1
    It is one of the secret test cases? – Yunnosch May 18 '19 at 07:39
  • Please make a [mcve]. – Yunnosch May 18 '19 at 07:39
  • Are you restricted not to use lib functions? I wonder why you do the string to number conversion yourself. – Yunnosch May 18 '19 at 07:42
  • Yes, It is from the secret test cases. Sorry, but didn't get your second comment. Should I upload a working example here ?? – samarendra chandan bindu Dash May 18 '19 at 07:42
  • I can use library functions but the inputs say the numbers lie in the range of 0 to 9 so always a single character. So instead of extracting a string, direct converting to integer was easier for me. – samarendra chandan bindu Dash May 18 '19 at 07:44
  • Did you read what the MCVE link is pointing to? Is there anything unclear about that detailed explanation? – Yunnosch May 18 '19 at 07:48
  • Okay I did. The problem is in the algorithm implementation function. Because the eval function was provided by the Coursera team. So the problem must be here. – samarendra chandan bindu Dash May 18 '19 at 07:59
  • So, if we don't know anything about the test it fails, how can we find a solution to it? – alx - recommends codidact May 18 '19 at 08:04
  • I know. :( I am facing the same problem right now. That's why I was asking if someone can suggest some test cases for which my solution won't work or some special boundary case that I might be omitting. It will be very helpful. I am working on the problem for over an week. Everything seems fine still the test case is not getting cleared. Don't even know the reason. – samarendra chandan bindu Dash May 18 '19 at 08:07
  • @samarendrachandanbinduDash did you try "6-2: Partitioning Souvenirs" problem? My 12th test case is failing, I have no idea why. Stress tests are passing fine, I used naive approach suggested by one of teaching assistants, so naive approach isn't wrong, which means my approach isn't wrong too. But 12th test case is still failing and this is one of the secret test cases. I can share my approach if you have tried it to see where I'm going wrong. – Zizou Mar 14 '20 at 07:51
  • 1
  • @samarendrachandanbinduDash here's the question I asked on stackoverflow which has explanation of my approach and code link in comments – Zizou Mar 14 '20 at 20:10

1 Answers1

1

I got the problem. It was integer overflow. I actually defined a1 and a2 as int inside the maximum and minimum function.

long long maximum(long long a,long long b,long long c,long long d)
{
    long long a1 = a>b?a:b;
    long long a2 = c>d?c:d;
    return a1>a2?a1:a2;
}

long long minimum(long long a,long long b,long long c,long long d)
{
    long long a1 = a<b?a:b;
    long long a2 = c<d?c:d;
    return a1<a2?a1:a2;
}

Changing it into long long solved the problem.