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:
If you can assume the numbers are positive, you can do this:
Example C#:
var bits = new bool[75];
foreach (var n in intArray)
{
if (n <= 75)
{
var diff = 75 - n;
if (bits[diff - 1])
{
MessageBox.Show(string.Format("Found pair: {0} and {1}", n, diff));
break;
}
bits[n - 1] = true;
}
}
But if the numbers in the array can be any valid integer, including negative numbers, you can do something like this:
var set = new HashSet();
foreach (var n in intArray)
{
if (n <= 75)
{
var diff = 75 - n;
if (set.Contains(diff))
{
MessageBox.Show(string.Format("Found pair: {0} and {1}", n, diff));
break;
}
set.Add(n);
}
}