Count pairs with given sum in C#

Solution 1:

// C# implementation of simple
// method to find count of
// pairs with given sum.
using System;

 internal class Program
 {
     static int[] arr = new int[] { 1, 2, 3, 4, 5 };
     static void Main(string[] args)
     {
         int sum = Convert.ToInt32(Console.ReadLine());
         int[] arr = { 1, 5, 7, -1, 5 };
         for(int i = 0; i < arr.Length; i++)
         {
             for(int j = i+1; j < arr.Length; j++)
             {
                 if(sum == arr[i] + arr[j])
                 {
                     Console.WriteLine("Sum pairs:" + i + "," + j);
                 }
             }
         }

         Console.ReadKey();
     }
 }
}

Output
Enter sum: 0
Sum pairs:0,3

Comments

Popular posts from this blog

Input = INDIA, count each char in input string