Удаление групп элементов из массива - C#
Формулировка задачи:
Из массива А удалить те цепочки четных элементов, в которых есть хотя бы один элемент из массива В
Помогите, пожалуйста..
Решение задачи: «Удаление групп элементов из массива»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] A = { 5, 6, 7, 4, 3, 2, 2, 2 };
int[] B = { 5, 2, 4 };
Console.WriteLine(string.Join(" ", A));
Console.WriteLine(string.Join(" ", B));
List<int> elementsToDelete = new List<int>();
for (int i = 0; i < A.Length; ) {
var elems = A.Skip(i).TakeWhile(x => x % 2 == 0);
if (elems.Count() == 0) { i++; continue; }
if (elems.Intersect(B).Count() != 0) { elementsToDelete.AddRange(elems); i += elems.Count(); }
else { i += elems.Count(); }
}
int[] newA = A.Except(elementsToDelete).ToArray();
Console.WriteLine(string.Join(" ", newA));
Console.ReadKey();
}
}
}