TwoSum Example in C#: Print and Count Pairs with Given Sum

homolibere

Nick

Posted on January 21, 2024

TwoSum Example in C#: Print and Count Pairs with Given Sum

In C#, the two-sum problem refers to finding pairs of integers in an array that sum up to a given target value. This is a common coding exercise that tests your ability to efficiently manipulate arrays and solve problems with two pointers.

In this post, we will discuss an example of how to implement a two-sum solution in C#. We will focus on printing and counting the pairs that satisfy the given sum.

First, let's define a method called FindPairs that takes an array of integers arr and a target sum target as parameters. This method will iterate through the array using two pointers, one starting from the beginning and the other from the end. For each comparison, we will determine if the elements sum up to the target and increment our count if they do.

public static int FindPairs(int[] arr, int target)
{
    int count = 0;
    int left = 0;
    int right = arr.Length - 1;

    Array.Sort(arr); // Sorting the array allows efficient two-pointer traversal

    while (left < right)
    {
        int currentSum = arr[left] + arr[right];

        if (currentSum == target)
        {
            count++;
            Console.WriteLine($"Pair found: {arr[left]}, {arr[right]}");
            left++;
            right--;
        }
        else if (currentSum < target)
        {
            left++;
        }
        else
        {
            right--;
        }
    }

    return count;
}
Enter fullscreen mode Exit fullscreen mode

In this method, we initialize our count, left pointer (left), and right pointer (right). We also sort our array using Array.Sort(arr) to optimize the two-pointer traversal.

Inside the while loop, we calculate the current sum of the elements at the left and right pointers. If the current sum is equal to the target sum, we increment our count, print the pair, and move both pointers toward the center. If the current sum is less than the target sum, we advance the left pointer to explore larger elements. Conversely, if the current sum is greater than the target sum, we decrement the right pointer to explore smaller elements.

Let's see an example usage of the FindPairs method:

public static void Main()
{
    int[] arr = { 1, 4, 2, 3, 5, 6, 7 };
    int target = 8;

    int count = FindPairs(arr, target);

    Console.WriteLine($"Total Pairs: {count}");
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have an array [1, 4, 2, 3, 5, 6, 7] and a target sum of 8. Running this code will output:

Pair found: 1, 7
Pair found: 2, 6
Pair found: 3, 5
Total Pairs: 3
Enter fullscreen mode Exit fullscreen mode

As we can see, the method successfully finds and prints the pairs that sum up to the target value and returns the count of pairs found.

In conclusion, implementing a two-sum solution in C# efficiently requires the use of two pointers and sorting the input array. The provided example demonstrates how to print and count such pairs using the two-pointer approach. Understanding and practicing these techniques will enhance your problem-solving skills in C#.

💖 💪 🙅 🚩
homolibere
Nick

Posted on January 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Demystifying Algorithms: Doubly Linked Lists
datastructures Demystifying Algorithms: Doubly Linked Lists

November 29, 2024

Demystifying Algorithms: Circular Linked Lists
datastructures Demystifying Algorithms: Circular Linked Lists

November 29, 2024

Demystifying Algorithms: Singly Linked Lists
datastructures Demystifying Algorithms: Singly Linked Lists

November 29, 2024

i18n e ASP.NET Core Web API
csharp i18n e ASP.NET Core Web API

November 28, 2024