Hi everyone I have just started learning Java and doing this exercise but I couldn’t understand how to write the code, I am not sure what POE is and higher index/lower index mean.; can anyone help me with explanation and writing the code.
*Consider an array A with n of positive integers. An integer idx is called a POE (point of equilibrium) of A, if A[0] + A[1] + … + A[idx – 1] is equal to A[idx + 1] + A[idx + 2] + … + A[n – 1]. Write a function to return POE of an array, if it exists and -1 otherwise.
They gave you a very mathematical description of the POE, but more or less it is the point where adding all of the values before that index is equal to adding up all of the values after that index. Suppose we have the following silly example: x=[1, 4, 5, 3, 2]
. In this case the POE would be index 2 (where the 5 is) because x[0] + x[1] = 1 + 4
is equal to x[3] + x[4] = 3 + 2
. I am not certain if you are mathematically guaranteed to have at most one POE if the array is exclusively positive integers, but that seems plausible (as you move through the indices, one side will be increasing and the other side will be decreasing). For the record, here is an example which does not have a POE: y=[1, 2, 3, 4, 5]
Can you think of a brute force solution that just checks every point in the array to determine if it is the POE? I can help you to optimize once you have a basic working solution.
Here is the basic template for the method:
public int findPointOfEquilibrium(int[] input) {
...
}
Also what I couldn’t understand is how to find POE and the meaning of A[idx-1] in the question
I just explained what POE means about as simply as possible.
And A[idx-1]
means the value in the array A
at index idx-1
.
Using the function I just wrote here are some basic requirements:
int[] first = {1, 4, 5, 3, 2};
// findPointOfEquilibrium(first) == 2;
int[] second = {1, 2, 3, 4, 5};
// No POE should return -1
// findPointOfEquilibrium(second) == -1;
int[] third = {5, 1, 2, 2, 1};
// findPointOfEquilibrium(third) == 1;