# About writing a code in java

**URL:** https://forum.codewithmosh.com/t/about-writing-a-code-in-java/15761
**Category:** Java
**Created:** [October 9, 2022, 1:30pm UTC](https://forum.codewithmosh.com/t/about-writing-a-code-in-java/15761 "2022-10-09T13:30:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Edi](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@Edi](https://forum.codewithmosh.com/u/Edi)
#### Post date: [October 9, 2022, 1:30pm UTC](https://forum.codewithmosh.com/t/about-writing-a-code-in-java/15761/1 "2022-10-09T13:30:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)
#### Post date: [October 9, 2022, 2:50pm UTC](https://forum.codewithmosh.com/t/about-writing-a-code-in-java/15761/2 "2022-10-09T14:50:38Z")

</div>

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:

```java
public int findPointOfEquilibrium(int[] input) {
  ...
}

```

---

<div class="post-metadata">

### Author: ![Edi](https://avatars.discourse-cdn.com/v4/letter/e/9de0a6/32.png) [@Edi](https://forum.codewithmosh.com/u/Edi)
#### Post date: [October 9, 2022, 5:37pm UTC](https://forum.codewithmosh.com/t/about-writing-a-code-in-java/15761/3 "2022-10-09T17:37:18Z")

</div>

Also what I couldn’t understand is how to find POE and the meaning of A[idx-1] in the question

---

<div class="post-metadata">

### Author: ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)
#### Post date: [October 9, 2022, 7:17pm UTC](https://forum.codewithmosh.com/t/about-writing-a-code-in-java/15761/4 "2022-10-09T19:17:33Z")

</div>

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:

```java
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;

```
