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
By in on Jan 06 2018
• Mar, 2018 16

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();

• 2
• May, 2018 12

 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; }

• 1
• Feb, 2018 14

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();

• 1
• Jun, 2020 8

 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(); }}

• 0
• Aug, 2019 10

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]); } } }

• 0
• Oct, 2018 7

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]){}}}

• 0
• Sep, 2018 10

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;}}

• 0
• Jan, 2018 31

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

• 0