Click here to Skip to main content
15,878,316 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
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
Comments
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?

1 solution

You must clean up and debug your code. Start with improved User Interaction.
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
 int maxLoad;
 int currentLoad;
}
Make more output, like in the comparision.

Visit some Learn C tutorial to solve your homework.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900