static bool findPairFromArray(int[] v,int suma,int n) {for (int i = 0; i < n; i++) {for (int j = 0; j < n && j != i; j++) {if (v[i] + v[j] == suma) {Console.WriteLine("The pair is :[{0},{1}]", v[i], v[j]);return true;}}}return false;}static void Main(string[] args){int[] v ={ 1, 2, 4, 7, 8,6,3 };//findPairFromArray(v, 8, 7);if (findPairFromArray(v, 10, 7)){Console.WriteLine("I find a pair!");}Console.ReadKey();}}
Interviews8 answers
Write an algorithm to find a pair of 2 elements to sum defined (user input) Eg Input array [1,2,3,6,8,1,6] and Sum = 5 so Expected output = [2,3] Explanation: only 2 and 3 forms a pair to sum = 5 in the given array. The answer should be generic for any array and sum input
8 Answers
Bran DavidPosted Jun 7, 2020, 10:58 PM EDT
public static void addaraay(){int[] addnum = { 2, 1, 6, 4, 8, 3, 2, 1 };string sum = Console.ReadLine();int totalsum = Convert.ToInt32(sum);int[] addnum1 = addnum;for (int i = 0; i <= addnum.Length - 1; i++){int j = i + 1;for (; j < addnum1.Length - 1; j++){int howmuch = addnum[i] + addnum1[j];if (howmuch == totalsum){Console.WriteLine("pair is " + addnum[i] +", " +addnum1[j]);}}}
Ravi FulaniPosted Aug 10, 2019, 6:46 PM EDT
int[] x = { 1, 2, 3, 6, 8, 1, 6 };
for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < x.Length; j++)
{
if (5 == x[i] + x[j])
{
}
}
}
MelbinPosted Oct 7, 2018, 6:52 AM EDT
Console.WriteLine("Enter array values with seperation , ");
string abc = Console.ReadLine();
int[] arr = new int[abc.Split(',').Length];
string[] strArray = abc.Split(',');
for (int i = 0; i < strArray.Length; i++)
{
arr[i] = Convert.ToInt32(strArray[i].ToString());
}
Console.WriteLine("Enter sum of number : ");
int sumofinput = Convert.ToInt32(Console.ReadLine());
//int[] SumnMinusOne
for (int i = 0; i < arr.Length; i++)
{
int ComplementaryNumber = sumofinput - arr[i];
if (arr.Contains(ComplementaryNumber))
{
Console.WriteLine("Sum of " + sumofinput + " pair is :" + arr[i] + "," + ComplementaryNumber);
break;
}
}
Rakesh MehtaPosted Sep 9, 2018, 10:43 PM EDT
public static int[] ArraySum(int[] arr,int sum){int len = arr.Length;int[] sumArr = null;for (int i = 0; i <; len; i++){for (int j = 0; j <; len; j++){if (i != j){if (arr[i] + arr[j] == sum){sumArr = new int[2];sumArr[0] = arr[i];sumArr[1] = arr[j];break;}}}if (sumArr != null) break;}return sumArr;}
kumar vPosted May 12, 2018, 5:39 AM EDT
Console.WriteLine("Enter array values with seperation : ");
string abc = Console.ReadLine();
int[] arr = new int[abc.Split(':').Length];
string[] strArray = abc.Split(':');
for (int i=0; i < strArray.Length;i++ )
{
arr[i] = Convert.ToInt32(strArray[i].ToString());
}
Console.WriteLine("Enter sum of number : ");
int sumofinput = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (Convert.ToInt32(arr[i]) + Convert.ToInt32(arr[j]) == sumofinput)
Console.WriteLine("Sum of " + sumofinput + " pair is :" + arr[i] + "," + arr[j]);
}
}
Console.Read();
NAVEEN KUMARPosted Mar 16, 2018, 2:19 AM EDT
Console.WriteLine("Enter max of array : ");
int[] arr = new int[Convert.ToInt32(Console.ReadLine())];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter sum of number : ");
int sumofinput = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (Convert.ToInt32(arr[i]) + Convert.ToInt32(arr[j]) == sumofinput)
Console.WriteLine("Sum of " + sumofinput + " pair is :" + arr[i] + "," + arr[j]);
}
}
Console.Read();
Rajesh KumarPosted Feb 13, 2018, 10:56 PM EST
sort array
set 2 pointer (p1,p2) to first and end of array
loop while p1 smaller than p2
check if p1+p2 is equla to number return true
check if p1+p2 is greater than number decrease p2
check if p1+p2 is equla to number increase p1
Amir BidvaPosted Jan 31, 2018, 11:51 AM EST

