Advertisement
Dianov

For Loop - Exercise (02. Half Sum Element)

Dec 27th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 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.  
  7. namespace HalfSumElement
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             int sum = 0;
  15.             int max = int.MinValue;
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 int number = int.Parse(Console.ReadLine());
  19.                 sum += number;
  20.                 if (number > max)
  21.                 {
  22.                     max = number;
  23.                 }
  24.             }
  25.             if ((sum - max) == max)
  26.             {
  27.                 Console.WriteLine("Yes");
  28.                 Console.WriteLine($"Sum = {max}");
  29.             }
  30.             else
  31.             {
  32.                 int diff = Math.Abs(max - (sum - max));
  33.                 Console.WriteLine("No");
  34.                 Console.WriteLine($"Diff = {diff}");
  35.             }
  36.         }  
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement