JulianJulianov

06. Equal Sums

Feb 6th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. 6. Equal Sums
  2. 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.
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _06EqualSum
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
  17.  
  18. bool isFound = false;
  19. for (int current = 0; current < array.Length; current++)
  20. {
  21. int sumRight = 0;
  22. for (int i = current + 1; i < array.Length; i++)
  23. {
  24. sumRight += array[i];
  25. }
  26. int sumLeft = 0;
  27. for (int i = current - 1; i >= 0; i--)
  28. {
  29. sumLeft += array[i];
  30. }
  31. if (sumRight == sumLeft)
  32. {
  33. Console.WriteLine(current);
  34. isFound = true;
  35. }
  36. }
  37. if (isFound == false)
  38. {
  39. Console.WriteLine("no");
  40. }
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment