15,960,736 members
See more:
A godown, which in other words called as a warehouse, is a building which is used to store raw materials or manufactured goods until they are exported to other places.

There are n number of go-downs which are used to store and ripe large quantity of bananas. Once the bananas are ripened, every godown owner will pack all those bananas as a single unit and transport them to airport for exporting them to other countries. All these godowns are close to each other and the owners are friendly. So while transporting the bananas, they would like to share the vehicle (if possible) in order to reduce the transportation cost. But only two people can share a vehicle and each vehicle can carry a weight of "w" tons at a time.

Given an array representing the weights of bananas of each owner, the maximum limit that the vehicle can hold, find the minimum number vehicles needed to transport all the bananas to the airport.
Note: There are no loads which are heavier than the given limit.
The weight of the bananas is measured in tons.

What I have tried:

getting wrong answer for the code
C
```#include <stdio.h>
#include <stdlib.h>

void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}

int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}

int minVehicles(int arr[], int n, int capacity) {
// Sort the array in non-decreasing order
qsort(arr, n, sizeof(int), compare);

// Use two pointers approach to count the number of vehicles needed
int count = 0;
int i = 0;
int j = n - 1;
while (i <= j) {
// If the sum of the current pair is less than or equal to the capacity, load both items
if (arr[i] + arr[j] <= capacity) {
i++;
j--;
} else {
// If not, load the heavier item in a separate vehicle
j--;
}
count++;
}

return count;
}

int main() {
int n, capacity;
printf("Enter the weights of the items separated by space: ");

// Read the weights of the items in a single line
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));

for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

clearInputBuffer(); // Clear the input buffer

printf("Enter the capacity of each vehicle: ");
scanf("%d", &capacity);

int result = minVehicles(arr, n, capacity);

printf("Minimum number of vehicles required: %d\n", result);

free(arr);

return 0;
}```
Posted
Updated 8-Dec-23 21:38pm
v2
Andre Oosthuizen 9-Dec-23 3:40am
"getting wrong answer for the code" does not help a lot. What is the expected output and what output are you getting?

## Solution 2

C++
```int main() {
int n, capacity;
printf("Enter the count of the items: ");

// Read the weights of the items in a single line
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));

for (int i = 0; i < n; i++) {
printf("Enter the weights of the item %d separated by space: ", i);
scanf("%d", &arr[i]);
}```

You better use some struct in your code for better readability, like for a vehicle
C++
```struct Vehicle {
int identifier; // like index
}```
Make more output, like in the comparision.

Visit some Learn C tutorial to solve your homework.