Prepin
Log in

Locate a pair within the array that adds up to the specified sum.

Approach / Explanation

This question pertains to data structures and algorithms and is commonly used to evaluate your programming and problem-solving abilities. The interviewer is requesting that you create a function or method that, given an array of integers and a target sum, finds and returns a pair of integers from the array that together equal the specified sum. Your solution should: Iterate through each element of the array. For each element, compute the difference between the target sum and the current element. This difference represents the value you need to locate in the rest of the array. Return the identified pair of integers. If no such pair is found, your function should communicate that accordingly. Additionally, take into account the time complexity of your solution. A brute force approach that compares each element with every other element would result in a time complexity of O(n^2), while an optimized approach utilizing a hash table would achieve a time complexity of O(n).

Suggested Answer

Below is a Python implementation for the problem utilizing a hash table: def find_pair ( arr , target_sum ) : if len ( arr ) < 2 : return "Array needs at least two numbers" num_map = { } for num in array : potential_match = target_sum - num if potential_match in num_map : return [ potential_match , num ] else : num_map [ num ] = True return "No pair found" arr = [ 3 , 5 , 2 , - 4 , 8 , 11 ] target_sum = 7 print ( find_pair ( arr , target_sum ) ) # Output: [2, 5] In this illustration, we first verify if the array contains at least two elements. If it does not, we return a message indicating "Array needs at least two numbers". Next, we loop through the array, saving each number in a hash table. For every number, we compute the complement needed to achieve the specified sum and check if that number is present in our hash table. If we find a match, we return these two numbers as a pair. If we have examined all numbers without finding a pair that sums to the target, we return "No pair found".

Alternative Answer

In Java, you can address this issue by utilizing a HashSet in the following manner: import java . util . HashSet ; public class Main { public static int [ ] findPair ( int arr [ ] , int sum ) { HashSet < Integer > hset = new HashSet < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int temp = sum - arr [ i ] ; if ( hset . contains ( temp ) ) { return new int [ ] { arr [ i ] , temp } ; } hset . add ( arr [ i ] ) ; } return new int [ ] { } ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 2 , - 4 , 8 , 11 } ; int sum = 7 ; int [ ] result = findPair ( arr , sum ) ; System . out . println ( result [ 0 ] + ", " + result [ 1 ] ) ; // Output: 5, 2 } } This Java example employs a method similar to the one used in the Python example, but it takes advantage of Java's built-in HashSet data structure for quick lookup times. When a pair is identified, it is returned as an array containing two integers. If no pair is found, an empty array is returned.

Question Details

Difficulty & Category

Medium

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.