Birthday Candles..wrong answer..please help

Can someone please tell me what is wrong with this code? It works fine for all the test cases in the example shown…Thanks in advance

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   int t,a[10],min,k,j,val;
   cin>>t;
   while(t)
    { 
      for(int i=0;i<10;i++)
         cin>>a[i];
      min=a[0];
      k=0;
      for(j=1;j<10;j++)
        {
            if(a[j]<min)
               { min=a[j];
                 k=j;
               }
        }
      if(min!=0)
        val=(k*pow(10,min))+k;
      else
        val=(k*pow(10,min));
      if(val==0)
       {
           min=a[1];
           k=1;
           for(j=2;j<10;j++)
            {
              if(a[j]<min)
               { min=a[j];
                 k=j;
               }
            }
        val=k*pow(10,min);
       }
       cout<<val<<endl;
       t--;
     
    };
   
   return 0;
}

@roshi Your code fails for following test case :

1
3 3 2 3 3 2 3 3 2 2

Correct answer for this test case is 222, but your code produces 202.

Here is your AC code :

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   int t,a[10],min,k,j,val;
   cin>>t;
   while(t)
   { 
      for(int i=0;i<10;i++)
         cin>>a[i];
      min=a[1];
      k=1;
      for(j=2;j<10;j++)
      {
         if(a[j]<min )
         { 
            min=a[j];
            k=j;
         }
      }
	  if (a[0] < min) 
	  {
	     k = 0; min = a[0];
	  }
      if (k == 0)
	     val = 1;
	  else 
	     val = 0;
	  for (int i = 0; i <= min; ++i) 
      {
         val = val * 10 + k;
      }
      cout<<val<<endl;
      t--;

  };

   return 0;
}

thank you argonaut…
I made this change in the code but still it shows a wrong answer.Could you please tell where i am going wrong?

     if(min!=0)
        {while(min>=0)
          { val+=(k*pow(10,min));
            min--;
          };
        }
     else
        val=(k*pow(10,min));

@roshi Consider we have found minimum value and index of minimum value, let minimum value = min and index of minimum value = k (not zero).
So our answer is kkkk…kkkk(min+1 times) i.e if k = 2 and min = 2 then answer is 222 (3 times).

Your code is producing different value. First do not use pow function, we just need to multiply val by 10 to shift digits of val to left and then add k at unit place.

Here is quick flow for min = 2 & k = 2:

Initially val = 0.

val = 0; Loop 1: val = 10 * 0 + 2; (val = val * 10 + k)
val = 2; Loop 2: val = 10 * 2 + 2;
val = 22; Loop 3: val = 10 * 22 + 2;

So finally we get val = 222.

Now if min value is for index 0 then initial value of val should be 1. Since we need to put min(value min) zeroes after 1. That is if min = 3 & k = 0 then answer val = 1000.

But there is one tricky case where we get min value for index 0 as well as some other index. Then we should consider smallest index other than 0 which have min value same as index 0.

Here is one such Case:

1
2 3 3 3 2 3 3 3 3 2

For this case answer must be 444 neither 1000 nor 999.

Hope it helps.

1 Like