wrong answer in adigit

#include<stdio.h>
//#include<conio.h>
int main()
{ int l,p,i,k,j,x,d,m,n,s;
unsigned int a[100002];
scanf("%d",&n);
scanf("%d",&m);
for(i=1;i<=n;i++)
{scanf("%d",&a[i]);}

   for(j=1;j<=m;j++)
   { 
		l=0;
    p=0;
	 scanf("%d",&x); 
	 for(k=1;k<x;k++)
	 {  s=a[x]-a[k];
	    if(s>0)
	   { l=l+s;}
	    else
	    {p=p+s;}
	    	}	
		d=l-p;
	printf("%d\n",d);
		}	   

// getch();
return 0;

scanf("%d",&a[i]);

Above line is incorrect. This will try to read a very long integer.
If you want to read single digit, better use

char ch;
scanf("%c", &ch);
a[i] = ch - '0';
1 Like

#include<stdio.h>
//#include<conio.h>
int main()
{ int l,p,i,k,j,x,d,m,n,s;

   int a[100002];
  scanf("%d",&n);
  scanf("%d",&m);	  
  for(i=1;i<=n;i++)
  { char ch;
		scanf("%c",&ch);
  a[i]=ch-'0';  
} 
   for(j=1;j<=m;j++)
   { 
		l=0;
    p=0;
	 scanf("%d",&x); 
	 for(k=1;k<x;k++)
	 {  s=a[x]-a[k];
	    if(s>0)
	   { l=l+s;}
	    else
	    {p=p+s;}
	    	}	
		d=l-p;
	printf("%d\n",d);
		}	   

// getch();
return 0;
}

//