mediumIntroduction to ArraysPattern: Brute Force

Pairs With Total High Calorie Solution

Problem Statement

You are given two arrays of integers representing the number of high-calorie bars in each ration pack. Find all pairs of ration packs that have a total number of high-calorie bars equal to a given target.

Examples

Example 1:
Input:[4, 2, 3, 1, 5] 27
Output:[[3, 4], [2, 5]]
Explanation: The pairs of ration packs are [3, 4] and [2, 5]. However, their totals are 7 and 10 respectively, not 27.

Constraints

  • The two input arrays can have different lengths.
  • The input arrays can have negative numbers.
  • The input arrays can have zeros.
  • The target number can be negative.
Time: O(n log n + m log m) Space: O(n + m)
Sort the two arrays and use the two-pointer technique to find all pairs of elements that sum to the target.

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.