Given a list of integers and a target sum, find all unique pairs of numbers that add up to the target. Return a list of strings where each string represents a pair in the format "num1,num2" with the smaller number first.

Your task is to implement a method that:

  • Accepts a list of integers and a target sum value
  • Finds all unique pairs that sum to the target
  • Returns pairs as formatted strings with the smaller number first
  • Returns an empty list if no pairs are found
  • Returns null if the input list is null

Examples:

Example 1:

List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5};
Integer target = 5;
List<String> result = findPairsWithSum(numbers, target);
// Returns: ["1,4", "2,3"]

Example 2:

List<Integer> numbers = new List<Integer>{3, 3, 3};
Integer target = 6;
List<String> result = findPairsWithSum(numbers, target);
// Returns: ["3,3"]

Example 3:

List<Integer> numbers = new List<Integer>{1, 2};
Integer target = 10;
List<String> result = findPairsWithSum(numbers, target);
// Returns: []

Example 4:

List<Integer> numbers = null;
Integer target = 5;
List<String> result = findPairsWithSum(numbers, target);
// Returns: null
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Feb 2026

Contest runs February 1 - 28. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.