Advertisement
petar088

6.EqualSums Java Fundamentals

Mar 20th, 2019
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. package Fundamentals._10_Exеrcise_ARRAYS;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class _6_EqualSums {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9.  
  10. int[] input = Arrays.stream(sc.nextLine().split(" "))
  11. .mapToInt(a -> Integer.parseInt(a)).toArray();
  12.  
  13. int[] arrayWithTwoZero = new int[input.length + 2];
  14.  
  15. for (int i = 1, j = 0; i <= input.length; i++, j++) { //добавяне на две нули, по една отпред и отзад!
  16. arrayWithTwoZero[i] = input[j];
  17. }
  18. arrayWithTwoZero[arrayWithTwoZero.length - 1] = 0;
  19. arrayWithTwoZero[0] = 0;
  20.  
  21. int sumLeft = 0;
  22. int sumRight = 0;
  23. int index = -1;
  24.  
  25.  
  26. for (int i = 0; i < arrayWithTwoZero.length - 2; i++) {
  27. sumLeft += arrayWithTwoZero[i];
  28. sumRight = 0;
  29. for (int j = arrayWithTwoZero.length - 1; (j > 0 && (i + 1) < j); j--) {
  30.  
  31. sumRight += arrayWithTwoZero[j];
  32.  
  33. if (sumLeft == sumRight && j - i == 2) {
  34. index = i;
  35. }
  36. }
  37. }
  38. if (index > -1) {
  39. System.out.println(index);
  40. } else if (index == -1) {
  41. System.out.println("no");
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement