Three Digit Sum Using Tuple in C#

Introduction

In this article, we will be discussing an algorithm which 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. }  
  • Above function which is return wist of Tuple<int,int,int> actually will contain three digit pairs that are equal to given sum.
  • We will iterate through for loop and check if the sum of the list is equal to the to 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;   
  • The below is the function which will do the same.

Final Code

The below is 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.   
  8.             List<Tuple< intintint >> pairs = PairsOfThreeDigitSum(lst, sum);  
  9.   
  10.             Console.WriteLine("Possible pairs of three digits whose sum are equivalent to {0} ", sum);  
  11.   
  12.             foreach (var _pairs in pairs)  
  13.             {  
  14.                 Console.WriteLine("{0} {1} {2}", _pairs.Item1, _pairs.Item2, _pairs.Item3);  
  15.             }  
  16.   
  17.             Console.ReadKey(true);  
  18.         }  
  19.   
  20.         public static List<Tuple<int,int,int>> airsOfThreeDigitSum(IList<int>        lstInt, int sum)  
  21.         {  
  22.            List<Tuple<int,int,int>> pairs = new List<Tuple<int,int,int>>();  
  23.            for (int i=0;i<lstInt.Count;i++)  
  24.            {  
  25.                for (int i=0;i<lstInt.Count;i++)  
  26.                {  
  27.                   for (int i=0;i<lstInt.Count;i++)  
  28.                   {  
  29.                       if (sum == (lstInt[i] + lstInt[j] + lstInt[k]))  
  30.                       {  
  31.                          pairs.Add(Tuple.Create<int,int,int>>(I , j, k);  
  32.                       }  
  33.                   }  
  34.                }  
  35.            }  
  36.            return pairs;     
  37.         }  
  38. }  

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.:)