Problem statementLet us first describe a SimpleFunction:SimpleFunction( string A,string B ){    int answer = 0;       for( int dig = 1; dig <= 9; dig++ ){            if(string A contains digit dig and string B contains digit dig){                answer = answer * 10 + dig;       }       return answer;}So basically this function receives two strings as an input and  returns a value.Akshara recently gained interest in coding and she came across this interesting question. She had 2 baskets. Each basket contained a  few strings. She wanted to find out what is the probability of  picking up  2 strings, 1st string from the first basket and the 2nd  string from the second basket, such that the value returned from the  Simple function would be an even value.Since she is not that good at Mathematics, she turns to you for help.  Please help her in solving this problem!Input: The first line contains an integer \$T\$ denoting the number of test cases. The first line of each test case contains 2  integers \$N1\$ and \$N2\$ denoting the size of first basket and  second basket respectively. This is followed up by \$(N1 + N2)\$  lines. Each line contains a string. The first \$N1\$ string correspond  to the first basket while the remaining to the second basket.Output:For each test case output the required probability correct up to 3  decimal places.Constraints:\$1 \le T \le 10\$\$1 \le N1, N2 \le 1000\$\$1 \le \text{Length of String} \le 1000\$Every String is composed of digits from [1 - 9]Sample input 12 2234526834533356498847675Sample output0.750ExplanationThe output of all the combination will be:Simple(234526,333564) = 3456Simple(8345,333564) = 345Simple(8345,98847675) = 458Simple(234526,98847675) = 456Since 3 of them are even, probability is \$\frac{3}{4}\$.Time Limit: 1.0 sec(s) for each input file.Memory Limit: 256 MBMy introduction of the algorithm:I asked the code review for my previous solution HackerEarth - SimpleFunction, and then the feedback is The code is hard to follow. Afterwards, I spent a few hours to work on the algorithm again, rewrite and passed all test code, and code is much simple. Here is the code link.I learned a few lessons through the practice. Most of important is to use counting sort to find two integers' (\$1 <= N <= 1000\$) last digit, avoid using time-consuming string manipulation. And also, avoid complicated code, jagged array with 3 dimension seems to be hard to follow in my previous solution. Most of important is that the time complexity is very easy to compute \$O(N1*N2)\$, \$N1, N2\$ are two integers in two baskets, both are less than \$1000\$.  The algorithm uses less memory, time is almost same as the previous one. The current algorithm's result on hackerearthThe previous algorithm's result on hackerearth.comI like to ask review again for the same algorithm.     using System;   using System.Collections.Generic;   using System.Diagnostics;   using System.Linq;   using System.Text;   using System.Threading.Tasks;   namespace SimpleFunction   {      /*       * Hackerearth.com simple function       * https://www.hackerearth.com/problem/algorithm/simple-function/       */     class Program     {       /*        * Use an integer array to express an integer from 1 to 1000, array size is 10.         * Store digit i from 1 to 9 if there is any digit i in the integer.         *         * For example, a number from 1 to 1000, 246 will be represented as int[]{0,0,1,0,1,0,1,0,0,0}.        *         * Design of hashed integer:         * 1. Number of same digits in the integer will not be saved, the information will be losted, for example,         * integers 1, 11, 111 will be saved to same array {0,1,0,0,0,0,0,0,0,0}        *         * 2. And also the order does not matter, 123, 213, 321 will be hashed to same array {0,1,1,1,0,0,0,0,0}        */       internal class HashedInteger       {          private static int SIZE = 10;           public int[] values;           public HashedInteger()          {            values = new int[SIZE];           }          /*           * @number - a string representing an integer from 1 to 1000,            */          public static HashedInteger Convert(string number)          {                            HashedInteger digits = new HashedInteger();            foreach (char c in number)            {                digits.values[ c- '0'] = 1;            }            return digits;          }          public static HashedInteger[] ConvertAll(string[] numbers)          {            if (numbers == null || numbers.Length == 0)                return new HashedInteger[] { };            int length = numbers.Length;            IList<HashedInteger> output = new List<HashedInteger>();            foreach (string number in numbers)            {                output.Add(HashedInteger.Convert(number));            }            return output.ToArray();          }                /*                         * Find largest digit in two numbers expressed in Digits           *            * return: last digit           */          public static int GetLastDigit(HashedInteger firstOne, HashedInteger secondOne)          {            int lastDigit = 0;            for (int i = 9; i >= 0; i--)            {                if (firstOne.values[i] == 1 && secondOne.values[i] == 1)                {                    lastDigit = i;                    break;                }            }            return lastDigit;          }       }               static void Main(string[] args)       {         ProcessInputs();         // RunSampleTestCase();        }       private static int RunSampleTestCase2()       {         return 0;       }       /*        * Sample test case in problem description        */       private static void RunSampleTestCase()       {        int[] baskets = new int[] { 2, 2 };        int numberInFirstBasket = baskets[0];        int numberInSecondBaseket = baskets[1];        string[] numbersInFirst = new string[] { 234526, 8345 };        string[] numbersInSecond = new string[] { 333564, 98847675 };        int count = CalculateSumOfEvenNumbers(numbersInFirst, numbersInSecond);        int totalCount = CalculateTotalCount(numbersInFirst, numbersInSecond);        double prob = count * 1.0 / totalCount;        Debug.Assert((prob.ToString(#.000).CompareTo(0.750)) == 0);      }      /*       * return value: probablity of even numbers compared to total pairs       */      private static void ProcessInputs()      {        int queries = Convert.ToInt32(Console.ReadLine());        for (int no = 0; no < queries; no++)        {            int[] baskets = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);            int numberInFirstBasket   = baskets[0];            int numberInSecondBaseket = baskets[1];            string[] numbersInFirst = new string[numberInFirstBasket];            string[] numbersInSecond = new string[numberInSecondBaseket];            for (int i = 0; i < numberInFirstBasket; i++)                numbersInFirst[i] = Console.ReadLine();            for (int i = 0; i < numberInSecondBaseket; i++)                numbersInSecond[i] = Console.ReadLine();            int count = CalculateSumOfEvenNumbers(numbersInFirst, numbersInSecond);            int totalCount = CalculateTotalCount(numbersInFirst, numbersInSecond);            double prob = count * 1.0 / totalCount;            Console.WriteLine(prob.ToString(N3));        }      }     /*      *       * Choose any number from each basket, compare two numbers to find largest digit in both numbers.       * If the largest digit is the even number, then add to the sum. Ruturn the value of sum of largest digit      * of a pair number from two baskets.       */      private static int CalculateSumOfEvenNumbers(string[] numbersA, string[] numbersB)      {        int count = 0;        HashedInteger[] shortNumbers1 = HashedInteger.ConvertAll(numbersA);        HashedInteger[] shortNumbers2 = HashedInteger.ConvertAll(numbersB);        foreach (HashedInteger firstOne in shortNumbers1)        {            foreach (HashedInteger secondOne in shortNumbers2)            {                                    int lastDigit = HashedInteger.GetLastDigit(firstOne, secondOne);                if (lastDigit % 2 == 0)                    count++;            }        }        return count;      }                                    private static int CalculateTotalCount(string[] numbersInFirst, string[] numbersInSecond)      {        return numbersInFirst.Length * numbersInSecond.Length;      }  }}
Hackerearth Simple Function - Follow-up
c#;algorithm;programming challenge;time limit exceeded