Split An Array Into Two Equal Sum Subarrays: This Java program aims to solve a problem involving dividing an integer array into two subarrays at a specific index, where the sum of elements in both subarrays is equal. Let’s break down the program step by step for beginners:
Split Array Into Two Equal Sum Subarrays
package com.softwaretestingo.interviewprograms; public class InterviewPrograms98 { // This Questions Asked In Unify technologies //Suppose this is interger array (2,3,4,1,4,5) divide this array into from the index of //that element where sum is equal for both divided arrays for eg:- //1 in this case should be printed as sum of both int arrays (2,3,4) and (4,5) is 9 public static void main(String[] args) { int a[] = {2,3,4,1,4,5}; int sum1=0; int sum2=0; int ans=0; for (int i = 0; i<=a.length-1; i++) { sum1=sum1+a[i]; for (int j = i+2; j<=a.length-1; j++) { sum2=sum2+a[j]; } if(sum1==sum2) { ans=a[i+1]; } sum2=0; } System.out.print(ans); } }
Output:
1
Alternative Way 1:
This Java program aims to find a split point in an array where the sum of the elements on the left side of the split point is equal to the sum of the elements on the right side. In other words, it tries to determine if the array can be divided into two parts with equal sums.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms98_1 { static int findSplitPoint(int arr[], int n) { int leftSum = 0 ; // traverse array element for (int i = 0; i < n; i++) { // add current element to left Sum leftSum += arr[i] ; // find sum of rest array // elements (rightSum) int rightSum = 0 ; for (int j = i+1 ; j < n ; j++ ) rightSum += arr[j] ; // split point index if (leftSum == rightSum) return i+1 ; } // if it is not possible to // split array into two parts return -1; } // Prints two parts after finding // split point using findSplitPoint() static void printTwoParts(int arr[], int n) { int splitPoint = findSplitPoint(arr, n); if (splitPoint == -1 || splitPoint == n ) { System.out.println("Not Possible"); return; } for (int i = 0; i < n; i++) { if(splitPoint == i) System.out.println(); System.out.print(arr[i] + " "); } } public static void main(String[] args) { int arr[] = {1 , 2 , 3 , 4 , 5 , 5 }; int n = arr.length; printTwoParts(arr, n); } }
Output:
1 2 3 4 5 5
Alternative Way 2:
A more effective strategy is to begin by calculating the sum of the entire array from left to right. Subsequently, we iterate through the array in reverse, simultaneously monitoring the sum on the right side. The sum on the left side can be derived by deducting the present element from the overall sum.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms98_2 { // Returns split point. If not possible, then // return -1. static int findSplitPoint(int arr[], int n) { // traverse array element and compute sum // of whole array int leftSum = 0; for (int i = 0 ; i < n ; i++) leftSum += arr[i]; // again traverse array and compute right // sum and also check left_sum equal to // right sum or not int rightSum = 0; for (int i = n-1; i >= 0; i--) { // add current element to right_sum rightSum += arr[i]; // exclude current element to the left_sum leftSum -= arr[i] ; if (rightSum == leftSum) return i ; } // if it is not possible to split array // into two parts. return -1; } // Prints two parts after finding split // point using findSplitPoint() static void printTwoParts(int arr[], int n) { int splitPoint = findSplitPoint(arr, n); if (splitPoint == -1 || splitPoint == n ) { System.out.println("Not Possible" ); return; } for (int i = 0; i < n; i++) { if(splitPoint == i) System.out.println(); System.out.print(arr[i] + " "); } } public static void main(String[] args) { int arr[] = {1 , 2 , 3 , 4 , 5 , 5 }; int n = arr.length; printTwoParts(arr, n); } }
Output:
1 2 3 4 5 5