Aashish Kumar
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 Aashish Kumar in Algorithms in C# on Jan 06 2018
  • NAVEEN KUMAR
    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
  • kumar v
    May, 2018 12

    1. public static int[] ArraySum(int[] arr,int sum)
    2. {
    3. int len = arr.Length;
    4. int[] sumArr = null;
    5. for (int i = 0; i <; len; i++)
    6. {
    7. for (int j = 0; j <; len; j++)
    8. {
    9. if (i != j)
    10. {
    11. if (arr[i] + arr[j] == sum)
    12. {
    13. sumArr = new int[2];
    14. sumArr[0] = arr[i];
    15. sumArr[1] = arr[j];
    16. break;
    17. }
    18. }
    19. }
    20. if (sumArr != null) break;
    21. }
    22. return sumArr;
    23. }

    • 1
  • Rajesh Kumar
    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
  • Bran David
    Jun, 2020 8

    1. static bool findPairFromArray(int[] v,int suma,int n) {
    2. for (int i = 0; i < n; i++) {
    3. for (int j = 0; j < n && j != i; j++) {
    4. if (v[i] + v[j] == suma) {
    5. Console.WriteLine("The pair is :[{0},{1}]", v[i], v[j]);
    6. return true;
    7. }
    8. }
    9. }
    10. return false;
    11. }
    12. static void Main(string[] args)
    13. {
    14. int[] v ={ 1, 2, 4, 7, 8,6,3 };
    15. //findPairFromArray(v, 8, 7);
    16. if (findPairFromArray(v, 10, 7))
    17. {
    18. Console.WriteLine("I find a pair!");
    19. }
    20. Console.ReadKey();
    21. }
    22. }

    • 0
  • Ravi Fulani
    Aug, 2019 10

    1. public static void addaraay()
    2. {
    3. int[] addnum = { 2, 1, 6, 4, 8, 3, 2, 1 };
    4. string sum = Console.ReadLine();
    5. int totalsum = Convert.ToInt32(sum);
    6. int[] addnum1 = addnum;
    7. for (int i = 0; i <= addnum.Length - 1; i++)
    8. {
    9. int j = i + 1;
    10. for (; j < addnum1.Length - 1; j++)
    11. {
    12. int howmuch = addnum[i] + addnum1[j];
    13. if (howmuch == totalsum)
    14. {
    15. Console.WriteLine("pair is " + addnum[i] +", " +addnum1[j]);
    16. }
    17. }
    18. }

    • 0
  • Melbin m p
    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
  • Rakesh Mehta
    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
  • Amir Bidva
    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


Most Popular Job Functions


MOST LIKED QUESTIONS