Introduction

In this article, we will be discussing an algorithm that returns possible (no. of pairs) digit pairs that are equal to a given number (6) using Tuple class.

Prerequisite

Suppose we pass a list of numbers List<int> { 1 , 2 , 3 , 4 , 5 , 6 } with 6 as a sum, The possible pairs should be :
  • 0 0 3 ( 1 + 1 + 4 ) = 6
  • 0 1 2 ( 1 + 2 + 3 ) = 6
  • 0 2 1 ( 1 + 3 + 2 ) = 6
  • 0 3 0 ( 1 + 4 + 1 ) = 6
  • 1 0 2 ( 2 + 1 + 3 ) = 6
  • 1 1 1 ( 2 + 2 + 2 ) = 6
  • 1 2 0 ( 2 + 3 + 1 ) = 6
  • 2 0 1 ( 3 + 1 + 2 ) = 6
  • 2 1 0 ( 3 + 2 + 1 ) = 6
  • 3 0 0 ( 4 + 1 + 1 ) = 6
C#

How?

  • The following is the basic function which we will implement in this article.
    1. public static List<Tuple<int,int,int>> PairsOfThreeDigitSum(IList<int> lstInt, int sum)
    2. {
    3. // Implementation
    4. }
  • The above function which is the return wist of Tuple<int,int,int> actually will contain three-digit pairs that are equal to a given sum.
  • We will iterate through for loop and check if the sum of the list is equal to the sum we gave.
    1. for (int i=0;i<lstInt.Count;i++)
    2. {
    3. for (int i=0;i<lstInt.Count;i++)
    4. {
    5. for (int i=0;i<lstInt.Count;i++)
    6. {
    7. if (sum == (lstInt[i] + lstInt[j] + lstInt[k]))
    8. {
    9. pairs.Add(Tuple.Create<int,int,int>>(I , j, k);
    10. }
    11. }
    12. }
    13. }
  • If condition becomes true it means this is the pair in which sum is equal to the sum we gave. I will store that pair index into our List of Tuples by Add() method
      1. if (sum == (lstInt[i] + lstInt[j] + lstInt[k]))
      2. {
      3. pairs.Add(Tuple.Create<int,int,int>>(I , j, k);
      4. }
      • Loop iterates until the count of elements in the list; and finally, it will return the pairs which hold three digits pairs as with,
        1. return pairs;
      • Below is the function which will do the same.

      Final Code

      Below are the code and output window.
      1. class Program
      2. {
      3. static void Main(string[] args)
      4. {
      5. var lst = new List<int> { 1, 2, 3, 4, 5 };
      6. int sum = 6;
      7. List<Tuple< int, int, int >> pairs = PairsOfThreeDigitSum(lst, sum);
      8. Console.WriteLine("Possible pairs of three digits whose sum are equivalent to {0} ", sum);
      9. foreach (var _pairs in pairs)
      10. {
      11. Console.WriteLine("{0} {1} {2}", _pairs.Item1, _pairs.Item2, _pairs.Item3);
      12. }
      13. Console.ReadKey(true);
      14. }
      15. public static List<Tuple<int,int,int>> airsOfThreeDigitSum(IList<int> lstInt, int sum)
      16. {
      17. List<Tuple<int,int,int>> pairs = new List<Tuple<int,int,int>>();
      18. for (int i=0;i<lstInt.Count;i++)
      19. {
      20. for (int i=0;i<lstInt.Count;i++)
      21. {
      22. for (int i=0;i<lstInt.Count;i++)
      23. {
      24. if (sum == (lstInt[i] + lstInt[j] + lstInt[k]))
      25. {
      26. pairs.Add(Tuple.Create<int,int,int>>(I , j, k);
      27. }
      28. }
      29. }
      30. }
      31. return pairs;
      32. }
      33. }

      Output

      C#

      Summary

      In this article, I discussed how we can create a method that returns Three-Digit Pairs using Tuple class in C#.
      I hope you liked my article.:)