Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajneniq;
- import java.util.Scanner;
- public class EqualSums {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read space-separated integers from the user
- String[] input = scanner.nextLine().split(" ");
- int[] arr = new int[input.length];
- for (int i = 0; i < input.length; i++) {
- arr[i] = Integer.parseInt(input[i]);
- }
- // Find and print the index where sums are equal
- System.out.println(findEqualSumIndex(arr));
- scanner.close();
- }
- public static String findEqualSumIndex(int[] arr) {
- for (int i = 0; i < arr.length; i++) {
- int leftSum = 0;
- int rightSum = 0;
- // Calculate sum of elements on the left side
- for (int j = 0; j < i; j++) {
- leftSum += arr[j];
- }
- // Calculate sum of elements on the right side
- for (int k = i + 1; k < arr.length; k++) {
- rightSum += arr[k];
- }
- // Check if sums are equal
- if (leftSum == rightSum) {
- return Integer.toString(i);
- }
- }
- return "no";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement