Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Given a list of numbers and a number k, return whether any two numbers from the list add up to k
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { int[] array1 = new int[] { 7,7,10,4,1,13}; int k = 20; var dup = array1.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(y => y.Key) .ToList(); List < int > l = new List < int > (); foreach(var item in array1) { if (array1.Contains(k - item)) { if((k - item)!=item) if (!l.Contains(item)) { Console.WriteLine(item.ToString() + "+" + (k - item).ToString()+"="+k); l.Add((k - item)); } if(dup.Contains(item) && (k - item)==item) if (!l.Contains(item)) { Console.WriteLine(item.ToString() + "+" + (k - item).ToString()+"="+k); l.Add((k - item)); } } } if(l.Count()<1) { Console.WriteLine("No additives found"); } } } }
run
|
edit
|
history
|
help
0
Length
Swap 2
newDiv2by1
normalised pagination
eze
XmlException in XmlSerializer.Deserialize when XML has \0
sdfrgthyjukioikujyhtgrfds
Encapsulation and abstration
sdfghyjujyhtgrfedcf
C