easyArraysPattern: Two Pointers

Tank Pair Allocation Solution

Problem Statement

You are given an array of tank capacities and an array of daily oxygen requirements. Find all pairs of tanks that can fulfill the requirements of two residents.

Examples

Example 1:
Input:[3, 2, 5], [6, 7, 5]
Output:[0, 2], [1, 2]
Explanation: Tanks of capacity 3 and 5 can fulfill the requirements of the residents with requirements 6 and 5, and tanks of capacity 2 and 5 can fulfill the requirements of the residents with requirements 7 and 5.
Example 2:
Input:[2, 3, 1], [7, 3, 5]
Output:[0, 1]
Explanation: There is no pair of tanks that can fulfill the requirements of all residents
Example 3:
Input:[5, 5, 5], [5, 5, 5]
Output:[0, 1], [1, 2], [2, 0]
Explanation: Each resident can be fulfilled by a different tank.

Constraints

  • 1 <= tankCapacities.length <= 10^5
  • 1 <= dailyRequirements.length <= 10^5
  • tankCapacities.length == dailyRequirements.length
Time: O(n log n) Space: O(n)
Use two pointers technique and sort the arrays of tank capacities and daily requirements to find pairs of tanks that can fulfill the requirements

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.