aspire12

06.EqualSum

Feb 11th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _06.EqualSum
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //Write a program that determines if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right(there will never be more than 1 element like that). If there are no elements to the left / right, their sum is considered to be 0.Print the index that satisfies the required condition or "no" if there is no such index.
  11.  
  12.  
  13.             int[] inputArray = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.             bool no = true;
  15.             for (int i = 0; i < inputArray.Length; i++)
  16.             {
  17.                 int leftSum = 0;
  18.                 for (int left = 0; left < i; left++)
  19.                 {
  20.                     leftSum += inputArray[left];
  21.                 }
  22.                 int rightSum = 0;
  23.                 for (int right = inputArray.Length - 1; right > i; right--)
  24.                 {
  25.                     rightSum += inputArray[right];
  26.                 }
  27.                 if(leftSum == rightSum)
  28.                 {
  29.                     Console.WriteLine(i);
  30.                     no = false;
  31.                     break;
  32.                 }
  33.             }
  34.             if (no)
  35.             {
  36.                 Console.WriteLine("no");
  37.             }
  38.         }
  39.     }
  40. }
Add Comment
Please, Sign In to add comment