Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 6. Equal Sums
- 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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _06EqualSum
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
- bool isFound = false;
- for (int current = 0; current < array.Length; current++)
- {
- int sumRight = 0;
- for (int i = current + 1; i < array.Length; i++)
- {
- sumRight += array[i];
- }
- int sumLeft = 0;
- for (int i = current - 1; i >= 0; i--)
- {
- sumLeft += array[i];
- }
- if (sumRight == sumLeft)
- {
- Console.WriteLine(current);
- isFound = true;
- }
- }
- if (isFound == false)
- {
- Console.WriteLine("no");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment