Advertisement
tsvetelinapasheva

TsvetelinaPasheva/ForLoopExercise/02.HalfSumElement

Feb 1st, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp15
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             int sum = 0;
  11.             int max = int.MinValue;
  12.             for (int i = 0; i < n; i++)
  13.             {
  14.                 int number = int.Parse(Console.ReadLine());
  15.                 sum += number;
  16.                 if (number > max)
  17.                 {
  18.                     max = number;                    
  19.                 }              
  20.             }
  21.             int sumWithoutMax = sum - max;
  22.  
  23.             if (sumWithoutMax == max)
  24.             {
  25.                 Console.WriteLine("Yes");
  26.                 Console.WriteLine($"Sum = {Math.Abs(max)}");
  27.             }
  28.             else
  29.             {
  30.                 Console.WriteLine("No");
  31.                 Console.WriteLine($"Diff = {Math.Abs(max - sumWithoutMax)}");
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement