Question bank › Two Pointers
Dsa Two Pointers Easy

The Pallet Loading Optimization

A warehouse needs to load exactly two crates onto a heavy-duty pallet. The pallet has a weight capacity C. You are given a list of crate weights already sorted in non-decreasing order. You want to pick two different crates such that their total weight is as close to C as possible without exceeding it. If multiple pairs have the same maximum sum, choose the pair with the smallest difference between the two weights (i.e., the weights are as balanced as possible). Input Format: Line 1: An integer C (the capacity). Line 2: A space-separated list of integers (the sorted crate weights). Output Format: Two space-separated integers (the weights of the two chosen crates) in non-decreasing order. If no pair fits, print -1. Example: Input: 100 10 25 30 45 50 60 Output: 45 50 (Explanation: 45+50=95 is the closest to 100. 30+60=90 and 45+50=95 are both <= 100, but 95 is closer.)

Key concepts

two_pointersgreedy

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.