-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b575d7
commit ea5c017
Showing
5 changed files
with
145 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
## 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 | ||
|
||
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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/algorithms/implementations/BalancedArraySumIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
src/main/java/algorithms/implementations/BalancedArraySumValidator.java
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/test/java/algorithms/implementations/BalancedArraySumIdentifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })); | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
src/test/java/algorithms/implementations/BalancedArraySumValidatorTest.java
This file was deleted.
Oops, something went wrong.