Find a pair in an array with a sum equal to the target using sorting.

Problem statement

Given array nums of n numbers and another number target, determines whether or not there exist two elements in nums whose sum is exactly equal to target.

Problem statement taken from:

Example 1:

Example 2:


The problem can be solved in O(NlogN) time, without using extra space. The NlogN is the average time taken to sort an array.

Sorting and two pointers


C++ solution

Golang solution

Originally published at

Software Engineer. Working Saeloun. My portfolio