Advertisement
grubcho

Equal sum after extraction (a => a == firstList[i]) - Lists

Jun 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You will be given two integer lists on the first two lines (space-separated). Remove the elements in the first list from the elements //in the second list. If an element from the first list occurs more than once in the second list, remove all occurrences.
  7. //After you remove the elements, check the sum of both lists. If the sum of both of them is equal, print β€œYes. Sum: {sum}”. If not //print the absolute difference between the two lists in the format β€œNo. Diff: {sum}”.
  8.  
  9. namespace Equal_Sum_After_Extraction
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             List<int> firstList = Console.ReadLine().Split().Select(int.Parse).ToList();
  16.             List<int> secondList = Console.ReadLine().Split().Select(int.Parse).ToList();
  17.             for (int i = 0; i < firstList.Count; i++)
  18.             {
  19.                 for (int j = 0; j < secondList.Count; j++)
  20.                 {
  21.                     if (firstList[i] == secondList[j])
  22.                     {
  23.                         secondList.RemoveAll(a => a == firstList[i]);
  24.                     }
  25.                 }
  26.             }
  27.          
  28.             int sumOfFirstList = firstList.Sum();
  29.             int sumOfSecondList = secondList.Sum();
  30.             if (sumOfFirstList == sumOfSecondList)
  31.             {
  32.                 Console.WriteLine($"Yes. Sum: {sumOfSecondList}");
  33.             }
  34.             else
  35.             {
  36.                 Console.WriteLine("No. Diff: {0}", Math.Abs(sumOfFirstList - sumOfSecondList));
  37.             }            
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement