Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
sum of two array elements
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { // Two Sum : sum of two array elements should be equal to the given target int[] nums = new int[] { 1, 4, 45, 6, 10, 8 }; int target = 16; Console.WriteLine("Target number is : " + target ); Console.WriteLine("Input array is : " ); Display(nums); int [] B = TwoSum(nums, target); Console.WriteLine("Result array is : " ); Display(B); } public static int[] TwoSum(int[] nums, int target) { int [] arr = new int[1]; HashSet<int> hs = new HashSet<int>(); for (int indx = 0; indx < nums.Length; ++indx) { int num1 = target - nums[indx]; if (hs.Contains(num1)) { arr = new int[] {num1, nums[indx]} ; } hs.Add(nums[indx]); } return arr; } // Display the array static void Display(int []arr) { int size = arr.Length; for (int indx=0; indx < size; ++indx) Console.Write(arr[indx] + " "); Console.WriteLine(); } } }
run
|
edit
|
history
|
help
0
project euler 14, C#
Random Number Generation
ArrayList list merge_collections
Find the count of subsets with given difference
2.2 gz
Random number generator
Fórum regex only alphanumerics (except underscore)
Prime Fibonacci
ex8 For_Loop
length