Loops Posted Sep 02, 2020

In this problem, we will be given a sorted set of numbers and a number, we need to print all the pairs available in the set whose sum is equal to the given number. For example, if we are given a set of numbers [1,2,5,6,7, 9,11,48,64, 84] and the sum is 8 - the output should be 1,7 2,6 since these are the only "pairs" of numbers which produce the given sum 8 among the numbers in the set.

This problem is similar to the other example Find all the pairs in a given unordered set of numbers whose sum is equal to a given input sum, except that in this case the given input is actually a sorted set instead of an unordered set.

Since we already know that the set is sorted in some order (ascending or descending), we can use our low-high approach we used in sorting the unordered set of binary numbers, where the low represents the starting indexes and high represents the ending indexes. As we move the low and high against each other we can find out which pair matches the given sum as we move.

**Logic:**

The approach goes as below:

- declare pointers low = 0, high = length (numbers) -1
- loop till low < high
- find the sum of numbers at low and high
- if the produced sum is less than expected, means that the sum should be on the higher side - so move the low towards right
- if the produced sum is higher than expected, means that the sum should be on the lesser side - so move the high towards left
- if the produced sum is equal to the expected, add this pair (low, high) to our output pairs and move the low and high indexes

**Code in C#:**

```
public void DoFindPairsForSumOrdered(
int[] arrayOfNumbers,
int expectedSum)
{
List<KeyValuePair<int, int>> expectedSumPairs
= new List<KeyValuePair<int, int>>();
int low = 0, high = arrayOfNumbers.Length - 1;
while (low < high)
{
int sum = arrayOfNumbers[low] + arrayOfNumbers[high];
if (sum == expectedSum)
{
expectedSumPairs.Add(
new KeyValuePair<int, int>(arrayOfNumbers[low], arrayOfNumbers[high]));
low++; high--;
}
else if (sum > expectedSum)
{
high--;
}
else if (sum < expectedSum)
{
low++;
}
}
foreach (var item in expectedSumPairs)
{
Console.WriteLine($"{item.Key},{item.Value}");
}
}
```

*This problem is based on the question used in Google Example Coding Interview Video

Enjoy this article?
*Buy Me a Coffee*

Join the community! Like on Facebook Follow on Twitter

Compute and generate a compressed string for a given string containing repetitions

Find all possible natural numbers below a given limit such that a3+b3 = c3+d3

Find all the pairs in a given ordered set of numbers whose sum is equal to a given input sum

Find all the pairs in a given unordered set of numbers whose sum is equal to a given input sum

Sort the given unordered set of Binary digits

(UPDATED to .NET 6) Implementing CQRS with MediatR in ASP.NET Core

(UPDATED to .NET 6) Calling Stored Procedures in ASP.NET Core / EF Core

(UPDATED to .NET 5) Integrating OData with ASP.NET Core Web API

Integrating MongoDB with ASP.NET Core (.NET 6)

Implementing Role based and Claims based Authorization in ASP.NET Core (.NET 5)