Interview Question Categories

B[i] = C[j] - A[k]

B[i] = C[j] - A[k]


/* Find all the combination of numbers from arrays
 * A, B and C if any B[i] = C[j] - A[k]
 */
namespace ThreeArraySumsProblem
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] A = { 1, 2, 3, 4, 5, 6 };
            int[] B = { 1, 2, 3, 4, 5, 6 };
            int[] C = { 1, 2, 3, 4, 5, 6 };

            HashSet<int> BElements = new HashSet<int>();

            foreach(int x in B)
            {
                BElements.Add(x);
            }

            for (int i = 0; i < C.Length; i++)
            {
                for (int j = 0; j < A.Length; j++)
                {
                    if (BElements.Contains(C[i] - A[j]))
                    {
                        Console.WriteLine(A[j] + "+" + (C[i] - A[j]) + " = " + C[i]);
                    }
                }
            }
        }
    }
}

No comments:

Post a Comment