package org.wesome.dsalgo;
public class EqualSumSplit {
static int equalSumSplit(int arr[]) {
int size = arr.length;
int sumFromRight = 0;
int sumFromLeft = 0;
int result = -1;
for (int i = 0; i < size; i++) {
sumFromLeft += arr[i];
}
for (int indx = size - 1; indx >= 0; indx--) {
sumFromRight = sumFromRight + arr[indx];
sumFromLeft = sumFromLeft - arr[indx];
if (sumFromRight == sumFromLeft) {
result = indx;
break;
}
}
if (result == -1 || result == size) {
System.out.println("Array cannot be split into equal sum sub array");
} else {
for (int i = 0; i < result; i++) {
System.out.print(arr[i] + " ");
}
System.out.print(" and ");
for (int i = result; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("\n");
}
return result;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.wesome.dsalgo.EqualSumSplit.equalSumSplit;
public class EqualSumSplitTest {
@Test
void equalSumSplitTest1() {
int arr[] = {1, 2, 3, 4, 5, 5};
Assertions.assertEquals(4, equalSumSplit(arr));
}
@Test
void equalSumSplitTest2() {
int[] arr = new int[]{3, 4, -1, 8};
Assertions.assertEquals(2, equalSumSplit(arr));
}
@Test
void equalSumSplitTest3() {
int[] arr = new int[]{-3, 1, -1, -1};
Assertions.assertEquals(2, equalSumSplit(arr));
}
@Test
void equalSumSplitTest4() {
int[] arr = new int[]{-3, 1, 0, -1, -1};
Assertions.assertEquals(3, equalSumSplit(arr));
}
@Test
void equalSumSplitTest5() {
int[] arr = new int[]{2, 3, 7, 6, 8, -1, -10, 15};
Assertions.assertEquals(7, equalSumSplit(arr));
}
}
plugins {
id 'java'
id "io.freefair.lombok" version "6.2.0"
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}