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!
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 July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.