# practice problem : birthday candles

Cant figure where my code is going wrong. giving 11 instead of 10 for 2nd sample input. .
#include <stdio.h>

int main(void) {
int i,small,pos,pos2,t,a[10],small2,flag,num;
scanf("%d",&t);
while(t–)
{ small=10;pos=10;pos2=10;small2=10;flag=0;num=0;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=9;i>=0;i–)
{
if(a[i]<=small)
{ small=a[i];
pos=i; }
}
if(pos==0)
{
for(i=1;i<10;i++)
{
if(a[i]==small)
{
pos2=i;
flag=1;
break;
}
}
if(flag==0)
{
for(i=9;i>=1;i–)
{
if(a[i]<=small2)
{pos2=i;
small2=a[i];
}
}
small++;
num=pos2*(10^small);
}
}
if((pos!=0)||(flag==1))
{
if(flag==1)
pos=pos2;
small++;
while(small!=0)
{
num=num*10+pos;
small–;
}
}
printf("%d\n",num);
}
return 0;
}

please post your code in proper blocks

The logic is flawed. It does not work when the chef has 1 each of all types of candles.
Please check the logic again.
Here is the code in proper blocks -

``````   int main(void)
{

int i,small,pos,pos2,t,a[10],small2,flag,num;

scanf("%d",&t);

while(t--)
{
small=10;
pos=10;
pos2=10;
small2=10;
flag=0;
num=0;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=9;i>=0;i--)
{
if(a[i]<=small)
{
small=a[i];
pos=i;
}
}
if(pos==0)
{
for(i=1;i<10;i++)
{
if(a[i]==small)
{
pos2=i;
flag=1;
break;
}
}
if(flag==0)
{
for(i=9;i>=1;i--)
{
if(a[i]<=small2)
{
pos2=i;
small2=a[i];
}
}
small++;
num=pos2*pow(10,small);
}
}
if((pos!=0)||(flag==1))
{
if(flag==1)
pos=pos2;
small++;
while(small!=0)
{
num=num+pos;
small--;
}
}
printf("%d\n",num);
}
return 0;
``````

}