Advertisement
YavorGrancharov

Equal_Sums

Jan 4th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Equal_Sums {
  4.     public static void main(String[] args) {
  5.         Scanner console = new Scanner(System.in);
  6.  
  7.         String line = console.nextLine();
  8.         String[] arr = line.split("\\s+");
  9.         int[] nums = new int[arr.length];
  10.  
  11.         for (int i = 0; i < arr.length; i++) {
  12.             nums[i] = Integer.parseInt(arr[i]);
  13.         }
  14.  
  15.         int leftSum;
  16.         int rightSum;
  17.         for (int i = 0; i <= nums.length - 1; i++) {
  18.             if (nums.length == 1) {
  19.                 System.out.println(0);
  20.                 return;
  21.             }
  22.             leftSum = 0;
  23.             for (int left = i; left > 0; left--) {
  24.                 int pos = left - 1;
  25.                 if (left > 0) {
  26.                     leftSum += nums[pos];
  27.                 }
  28.             }
  29.             rightSum = 0;
  30.             for (int right = i; right < nums.length; right++) {
  31.                 int pos = right + 1;
  32.                 if (right < nums.length - 1) {
  33.                     rightSum += nums[pos];
  34.                 }
  35.             }
  36.             if (leftSum == rightSum) {
  37.                 System.out.println(i);
  38.                 return;
  39.             }
  40.         }
  41.         System.out.println("no");
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement