Advertisement
Guest User

_11.Equal_Sums

a guest
May 28th, 2016
2,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 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 _11.Equal_Sums
  8. {
  9. class Program
  10. {
  11. private static int pos = 0;
  12. private static bool isExist = false;
  13. static void Main(string[] args)
  14. {
  15. int[] arr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  16.  
  17. for (int i = 0; i < arr.Length; i++)
  18. {
  19. int leftSum = SumLeft(arr, i);
  20. int rightSum = SumRight(arr, i);
  21. if (leftSum == rightSum)
  22. {
  23. Console.WriteLine(i);
  24. return;
  25. }
  26.  
  27. }
  28. Console.WriteLine("no");
  29. }
  30.  
  31. private static int SumLeft(int[] arr, int position)
  32. {
  33. int leftSum = 0;
  34.  
  35. for (int i = position; i > 0; i--)
  36. {
  37. int nextPosition = i - 1;
  38. if (i==0 )
  39. {
  40. leftSum += 0;
  41. break;
  42. }
  43. else
  44. {
  45. leftSum += arr[nextPosition];
  46.  
  47. }
  48. }
  49. return leftSum;
  50.  
  51. }
  52.  
  53. private static int SumRight(int[] arr, int position)
  54. {
  55. int rightSum = 0;
  56.  
  57. for (int i = position; i < arr.Length; i++)
  58. {
  59. int nextPosition = (i +1 )%arr.Length;
  60. if (arr.Length == 1 || nextPosition == 0)
  61. {
  62. rightSum += 0;
  63.  
  64. break;
  65. }
  66. else
  67. {
  68. rightSum += arr[nextPosition];
  69.  
  70. }
  71. }
  72. return rightSum;
  73.  
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement