# How to count the highest left and highest right heights?

I have to write a method that counts the highest left and highest right heights of the current index. Let's say we have the array {1,4,7,3,2}, so for index 1 ( height 4) - the `maxLeftHeight=1` , `maxRightHeight=7` and for index 2(height 7) - `maxLeftHeight=4`, `maxRightHeight=3` and so on...

The first and the last indexes don't count so the array starts counting at 1 and finishes counting at `array.length-1`

I tried to write a code for this and this is what I got:

``````public String getCurrentMaxHeights(int[] height)
{
int maxLeft = 0;
int maxRight = 0;
String st = "";
for (int i = 1; i < height.length - 1; i++)
{

maxRight = Math.max(height[i + 1], height[height.length - 1]);
maxLeft = Math.max(height[i - 1], height[0]);
st += "The max left height for " + height[i] + " is " + maxLeft +
" the max right height is " + maxRight + " \n";

}
return st;
}
``````

output:

The max left height for 1 is 2 the max right height is 3 The max left height for 1 is 2 the max right height is 4 The max left height for 4 is 2 the max right height is 3 The max left height for 1 is 4 the max right height is 3 The max left height for 1 is 2 the max right height is 3 The max left height for 2 is 2 the max right height is 3

This is wrong, how can I fix it to be right?

On

Try this `selectedIndex` is the `index` you start from, Moreover, add check if there is values on the `left` or the `right` or not

``````  Integer[] leftValues = Arrays.copyOfRange(numArray, 0, selectedIndex);
Integer[] rightValues = Arrays.copyOfRange(numArray, selectedIndex+1,numArray.length);
List<Integer> leftList = Arrays.asList(leftValues);
List<Integer> rightList = Arrays.asList(rightValues);
int leftMax =
leftList.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMax();
int rightMax=
rightList.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMax();
``````

Another way with complete example if you want it with `int array` not `Integer`:

``````  int[] spam = new int[] { 1, 2, 3,5,4,7,1,2,9,12 };
int[] leftValues = Arrays.copyOfRange(spam, 0, 7);
int[] rightValues = Arrays.copyOfRange(spam, 7+1,spam.length );
List<Integer> leftList = Arrays.stream(leftValues).boxed().collect(Collectors.toList());
List<Integer> rightList = Arrays.stream(rightValues).boxed().collect(Collectors.toList());
int leftMax = leftList.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMax();
int rightMax=
rightList.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMax();
``````
On

You need to use `2 nested for-loops`:

• One for getting the `maxLeft`.
• One for getting the `maxRight.`

Where `i` is the current index, run from `0` to `i - 1`, calculating `maxLeft` and run from `i + 1` to `n - 1`, calculating `maxRight`.

``````public String getCurrentMaxHeights(int[] height)
{
int maxLeft;
int maxRight;
String st = "";
for (int i = 1; i < height.length - 1; i++)
{
// Initialize to first element.
maxLeft = height[0]
// Update if any are greater.
for (int j = 1; j < i; j++)
maxLeft = Math.max(maxLeft, height[j])
// Initialize to element following i.
maxRight = height[i + 1]
// Update if any are greater.
for (int j = i + 2; j < height.length; j++)
maxRight = Math.max(maxRight, height[j])

st += "The max left height for " + height[i] + " is " + maxLeft +
" the max right height is " + maxRight + " \n";
}
return st;
}
``````