Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

Bruce is shifting things from his old home to the new one

Computer Science Sep 06, 2022

Bruce is shifting things from his old home to the new one. He can carry at most 2 objects in one move, provided the sum of the weight of those objects is at most ‘k’. If the sum exceeds 'k', then Bruce will carry only 1 object. No object has a weight greater than 'k'. Return the minimum number of moves he’ll require to shift all the objects.

You’re given an integer array arr of N size, containing the weights of different objects, and an integer k, representing the maximum weight he can carry at a time.

Input format –

    • First line will contain the size of the array, N

    • Next ‘N’ lines will contain N integers denoting array arr.

    • The last line will contain integer k.

note
Constraints
    • 1<=N<=1000

    • 1<=arr[i]<=k<=1000

view_list
Examples
Input:
4

3

2

2

1

3

Output:
3

Explanation:
arr = {3, 2, 2, 1}, k = 3

Move 1 -> carry 3

Move 2 -> carry 2 + 1

Move 3 -> carry 2

Answer -> 3 moves required.

Expert Solution

int shifting(vector arr, int k)
{
    int i,j,n=arr.size(),c=0;
    sort(arr.rbegin(),arr.rend());
    i=0;
    j=n-1;
    while(i     {
        c++;
        if(arr[i]+arr[j]<=k)
        {
            i++;
            j--;
        }
        else
        {
            i++;
        }
    }
    if(i==j)
        c++;
    
    return c;
    
}

Archived Solution
Unlocked Solution

You have full access to this solution. To save a copy with all formatting and attachments, use the button below.

Already a member? Sign In
Important Note: This solution is from our archive and has been purchased by others. Submitting it as-is may trigger plagiarism detection. Use it for reference only.

For ready-to-submit work, please order a fresh solution below.

Or get 100% fresh solution
Get Custom Quote
Secure Payment