Skip to content

Commit

Permalink
Merge 86f7a62 into d7e9614
Browse files Browse the repository at this point in the history
  • Loading branch information
jaebradley committed Aug 22, 2017
2 parents d7e9614 + 86f7a62 commit fd0e1ff
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
58 changes: 58 additions & 0 deletions codereview/balancedArraySumIdentifier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## Problem

Extended from [this HackerRank problem](https://www.hackerrank.com/challenges/sherlock-and-array).

> Given an array `A` of length `n`, determine if there exists an element in the array such that the sum of the elements on
its left is equal to the sum of the elements on its right.
> If there are no elements to the left/right, then the sum is considered to be zero.
For example, `[ 1, 1, 1, 2 ]`, the sum of elements to the left and right of index `2` are equal.

## Approach

The following description is for the left-to-right evaluation of the array of values. In my implementation, I do both the
left-to-right and right-to-left evaluations in the same loop.

1. Given an array of values, start with the first and last index. These will be the initial values of the left and
right sums and the initial left and right indices
2. Increment the left index by `1` and decrement the right index by `1`
1. If the left index is less than the right index, add the value at the right index to the right sum
2. If the left index is greater than the right index, subtract the value at the left index from the right sum
3. If the left and right sums are the same, then `return` early
4. Else, add the value at the left index to the left sum
3. After exiting the loop, check if the left sum is equal to the right sum

## Implementation

<!-- language: lang-java -->

public class BalancedArraySumIdentifier {
public static boolean isArraySumBalanced(int[] values) {
int leftToRightLeftSum = 0;
int leftToRightRightSum = 0;

int rightToLeftRightSum = 0;
int rightToLeftLeftSum = 0;

for (int i = 0; i < values.length; i++) {
int j = values.length - 1 - i;

if (i < j) {
leftToRightRightSum += values[j];
rightToLeftLeftSum += values[i];
} else if (i > j) {
leftToRightRightSum -= values[i];
rightToLeftLeftSum -= values[j];
}

if (leftToRightLeftSum == leftToRightRightSum || rightToLeftLeftSum == rightToLeftRightSum) {
return true;
}

leftToRightLeftSum += values[i];
rightToLeftRightSum += values[j];
}

return leftToRightLeftSum == leftToRightRightSum || rightToLeftLeftSum == rightToLeftRightSum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package algorithms.implementations;

/**
* https://www.hackerrank.com/challenges/sherlock-and-array?h_r=next-challenge&h_v=zen
*
* Given an array A of length n, determine if there exists an element in the array such that the sum of the elements on
* its left is equal to the sum of the elements on its right.
*
* If there are no elements to the left/right, then the sum is considered to be zero.
*/

public class BalancedArraySumIdentifier {
public static boolean isArraySumBalanced(int[] values) {
int leftToRightLeftSum = 0;
int leftToRightRightSum = 0;

int rightToLeftRightSum = 0;
int rightToLeftLeftSum = 0;

for (int i = 0; i < values.length; i++) {
int j = values.length - 1 - i;

if (i < j) {
leftToRightRightSum += values[j];
rightToLeftLeftSum += values[i];
} else if (i > j) {
leftToRightRightSum -= values[i];
rightToLeftLeftSum -= values[j];
}

if (leftToRightLeftSum == leftToRightRightSum || rightToLeftLeftSum == rightToLeftRightSum) {
return true;
}

leftToRightLeftSum += values[i];
rightToLeftRightSum += values[j];
}

return leftToRightLeftSum == leftToRightRightSum || rightToLeftLeftSum == rightToLeftRightSum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package algorithms.implementations;

import org.junit.Test;

import static org.junit.Assert.*;

public class BalancedArraySumIdentifierTest {
@Test
public void itShouldFindBalancedArraySum() {
assertFalse(BalancedArraySumIdentifier.isArraySumBalanced(new int[] { 1, 1, 1, 3 }));
}

@Test
public void itShouldNotFindBalancedArraySum() {
assertFalse(BalancedArraySumIdentifier.isArraySumBalanced(new int[] { 1, 1, 1, 1 }));
}

@Test
public void itShouldReturnFalseForEmptyArray() {
assertTrue(BalancedArraySumIdentifier.isArraySumBalanced(new int[] {}));
}

@Test
public void itShouldReturnTrueForSingleArray() {
assertTrue(BalancedArraySumIdentifier.isArraySumBalanced(new int[] { 3 }));
}

@Test
public void itShouldReturnTrue() {
assertTrue(BalancedArraySumIdentifier.isArraySumBalanced(new int[] { 1, 2, 3, 3 }));
}

@Test
public void itShouldReturnTrueForMiddle() {
assertTrue(BalancedArraySumIdentifier.isArraySumBalanced(new int[] { 3, 2, 3 }));
}

@Test
public void itShouldReturnFalse() {
assertFalse(BalancedArraySumIdentifier.isArraySumBalanced(new int[] { 1, 2, 3 }));
}

@Test
public void itShouldReturnTrueForNegatives() {
assertTrue(BalancedArraySumIdentifier.isArraySumBalanced(new int[] { 1, 1, -1, -1 }));
}
}

0 comments on commit fd0e1ff

Please sign in to comment.