Find in list the entities have SUM= k
ex: [1,5,8,1,2]
k = 13
should return 5,8
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] numbers = new int[5]{1,5,8,3,7};
int k = 13;
var originearray = numbers;
var toReturn = new List<int>();
int sum = 0;
while(sum != k && numbers.Length > 0){
for(int i =0; i< numbers.Length ; i++){
var tmp = sum;
sum = sum + numbers[i];
if(sum <= k){
toReturn.Add(numbers[i]);
}
if(sum > k){
sum = tmp;
}
}
if(sum != k){
sum =0;
numbers = numbers.Skip(1).ToArray();
toReturn = new List<int>();
}
}
foreach(var item in toReturn){
Console.WriteLine(item);
}
if(sum == k){
Console.WriteLine("Found");
}
else{
Console.WriteLine("Not Found");
}
var possitions = new List<int>();
for(int i = 0; i<toReturn.Count; i++ ){
for(int j = 0; j < originearray.Length; j++){
if(toReturn[i] == originearray[j] && !possitions.Contains(j)){
possitions.Add(j);
}
}
}
foreach(var item in possitions){
Console.WriteLine(item);
}
}
}
Pogo Stick, bâton Sauteur pour Enfants à partir de 5 Ans, Conception Tendance, Poids Max. 50 kg