We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Nice work! However, I don't think you need to reverse the arrays when you can just iterate backwards. Below is my java version, but I gotta say ... this is another problem that is not categorized or explained clearly. Symbolically these are stacks but the solution doesn't require stack data structures. In fact utilizing one would make the solution more complicated (and may still require an auxiliary DS). Second, Nabila expects the input stack values in reverse order (at least from a LIFO perspective). Not sure if this was on purpose to try and trick people, or just a weird way of thinking about stacks.
static int equalStacks(int[] h1, int[] h2, int[] h3) {
if(data == null || data.length < 1) return new int[0];
int[] sums = new int[data.length];
int last = data.length - 1;
int sum = 0;
// walk input array backwards (since it's supposed to be a "stack", but
// generate sum array in normal order to facilitate binary search
for(int i = last, j = 0; i >= 0 && j < sums.length; i--, j++) {
sum += data[i];
sums[j] = sum;
}
return sums;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Nice work! However, I don't think you need to reverse the arrays when you can just iterate backwards. Below is my java version, but I gotta say ... this is another problem that is not categorized or explained clearly. Symbolically these are stacks but the solution doesn't require stack data structures. In fact utilizing one would make the solution more complicated (and may still require an auxiliary DS). Second, Nabila expects the input stack values in reverse order (at least from a LIFO perspective). Not sure if this was on purpose to try and trick people, or just a weird way of thinking about stacks.
static int equalStacks(int[] h1, int[] h2, int[] h3) {
}
private static int[] calculateSums(int[] data) {
}