Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
return whether any two numbers from the list add up to k
//Rextester.Program.Main is the entry point for your code. Don't change it. //Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354) using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { /* This problem was recently asked by Google. Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. Bonus: Can you do this in one pass? */ var numbers=new[]{10,15,3,7}; var k=17; var additive = FindAdditives(numbers,k); if(additive==null){ Console.WriteLine($"Additive not found for {k}"); return; } Console.WriteLine($"Found two numbers {additive.OP1} and {additive.OP2} that adds to {k}"); } public static Additives FindAdditives(int[] numbers, int k){ var hashSet=new HashSet<int>(); foreach(var num in numbers){ var op1=num; var op2= Math.Abs(k-num); if(hashSet.Contains(op2)) return new Additives(op1,op2); hashSet.Add(op2); } return null; } } public class Additives { public Additives(int op1, int op2) { OP1=op1; OP2=op2; } public int OP1{get;} public int OP2{get;} } }
run
|
edit
|
history
|
help
0
Collection_ basic datastructure brushup
WebScrape using Regex
Q: Why this calling base class virsion?
contraseña
sdfrgthyjuiloiujyhtgrf
Serialization
Fórum Group Data Within 5 Hours Maximum
Index.cshtml
prime number experiment
azsdfghju654 ffddfdf