Coding problem
Two positions that add up to a target
The task
A gift-card checkout looks for two items whose prices add up exactly to the card's value.
Complete FindPair(int[] numbers, int target). Return the zero-based positions of two different elements whose values add up to target, the smaller position first, or null if no such pair exists. The same element cannot be used twice, but two different elements may have equal values. The array is never null, there is at most one valid pair, and every possible pair sum fits in an int.
Signature
public static (int First, int Second)? FindPair(int[] numbers, int target)
Examples
[4, 9, 13, 20], target 13 →(0, 1). 4 + 9 = 13.[1, 2, 4], target 8 →null. No two values add up to 8; 4 + 4 would use the same element twice.
Limits: Up to 400,000 values.
The tests check: an empty array, one element, no matching pair, equal values at different positions, negative values, zero in a pair, the order of the two positions, a pair that ends at the last element, and 400,000 values in time.
Hints
The solution opens by itself when all tests pass. You remember more if you try first.
Sign in to see the solutionYour code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// Returns the two positions in ascending order, or null if no pair exists.
public static (int First, int Second)? FindPair(int[] numbers, int target)
{
throw new NotImplementedException();
}
}Sign in to run and submit your code. It stays here while you do.