Find if pair of elements with given sum exists in large integer array

前端 未结 5 2035
独厮守ぢ
独厮守ぢ 2021-01-15 07:45

I\'m having a integer array of 10 Million elements, how to write a function in C# which returns True if the array has a pair which sums up to 75.

My code is:

5条回答
  •  醉梦人生
    2021-01-15 08:21

    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Collections.Generic;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                const int max = 10000000;
                const int sum = 75;
    
                var data = new int[max];
    
                var rnd = new Random();
    
                bool found = false; 
                int c = 1;
                Stopwatch sw;
    
                while (!found)
                {
                    sw = Stopwatch.StartNew();
                    for (int i = 0; i < max; ++i)
                    {
                        data[i] = rnd.Next(0, max*25);
                    }
                    sw.Stop();
                    Console.WriteLine("Took {0}ms to create the array", sw.ElapsedMilliseconds);
    
                    sw = Stopwatch.StartNew();
                    var check75 = new HashSet(data.Where(x => x <= 75));
                    sw.Stop();
                    Console.WriteLine("Took {0}ms to create the hashset", sw.ElapsedMilliseconds);
    
                    sw = Stopwatch.StartNew();
                    for (int i = 0; i < max; ++i)
                    {
                        if (check75.Contains(sum - data[i]))
                        {
                            Console.WriteLine("{0}, {1} ", i, data[i]);
                            found = true;
                        }
                    }
                    sw.Stop();
                    Console.WriteLine("Took {0}ms to check75", sw.ElapsedMilliseconds);
    
                    Console.WriteLine("Loop #" + c++);
                }
                Console.WriteLine("Finish");
                Console.ReadKey();
            }
        }
    }
    

提交回复
热议问题