There are two trucks on the roads and you have n boxes. You want to carry all of the boxes from Dhaka to Sylhet using these two trucks. Remember, you must use these two trucks together. You know the weight of each box. One of the truck drivers is very lazy to start the truck so that he hit upon a plan. He said to you, “If the load of two trucks is not equal, then they will not start the trucks”. Can you please figure out whether it is possible to bring those n boxes to Sylhet using those two trucks? The first line of the input represents n (2<=n<=1000), the number of boxes, and the following line represents the weight of those n boxes. The maximum weight of each box can be 100. Print YES if it is possible to divide the boxes in such a way that the load of two trucks is equal, otherwise print NO.
Sample Input 1:
5 2 3 6 2 1
Sample Output 1:
YES
Sample Input 3:
4 2 4 2 2
Sample Output 3:
NO
using namespace std;
int DivisionSpot(int arr[], int n)
{
int lSum = 0,rSum=0 ;
int i, j;
for (i = 0; i < n; i++)
{
lSum += arr[i] ;
rSum = 0 ;
for (j = i+1 ; j < n ; j++ ) rSum += arr[j] ;
if (lSum == rSum) return i+1 ;
}
return -1;
}
void CheckDivision(int arr[], int n)
{
int P = DivisionSpot(arr, n),i;
if (P == -1 || P == n )
{
cout <<"NO" <<endl;
return;
}
cout<<"\nYES\n";
for (i = 0; i < n; i++)
{
if(P == i) cout <<endl;
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = {5, 2, 3, 6, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
CheckDivision(arr, n);
return 0;
}
Comments
Leave a comment