Can someone tell me where I went wrong that my code doesn't work


#1
int i=0,j=0,sum=0,q=0,k=0,l=0,z=0;
int size = 2*B;
int arr[size];

//New array from First B elements and Last B elements of array A
for(j=0;j<B;j++)
{
    arr[j]=A[j];
    i++;
}
for(l=0;l<B;l++)
{
    arr[i]=A[n1-l-1];
    i++;
}
  
//for(k=0;k<size;k++)
//{
//    printf("%d\n",arr[k]);
//}

int max[B], temp=0, m=0,n=0,p=0;

//Arranging the new array in decreasing order
for(m=0; m< size-1; m++)
{
    for(n=m+1;n<size; n++)
    {
        if(arr[m]<arr[n])
        {
            temp=arr[m];
            arr[m]=arr[n];
            arr[n]=temp;
            
        }
        
    }
}
//Taking the sum for first B elements from new array
for(p=0; p<B; p++)
{
    sum=sum+arr[p];
}

// printf("%d",sum);
return sum;


#2

I think u misread the ques the question is u need to take consecutive elements from both sides but if u take b elements from both end then make one array out of it and then add…then elements will not remain consecutive for both sides…so what u are doing is finding maximum sum of last b and first b elements taking only b elements…but the ques is to find taking b elemnts n total from both ends…
e.g-
a[]={1, 2,-1,3} b=2
so soln can be either zero element from front and 2 from back or
one lement from front and 1 from back or
2 elemebd from front and 0 from back…
try this appoach …also i will suggest u to use java or c++ to code coz there are in built funcitons to sort there…Hope this helps…Happy coding!!