# Find Array Elements Which are Greater than its Left Elements in Java?

An array is a linear data structure in which elements are stored in contiguous memory locations.

As per the problem statement, we have given an array with some random integer values and we have to find out the numbers which are greater than its all left elements by using Java programming language.

Let’s start!

## To show you some instances

### Instance-1

Given Array= [1, 2, -3, -4, 0, 5].

The numbers which are greater than its all left elements = 2, 5

### Instance-2

Given Array= [-1, 0, 4, 6, 8, -5].

The numbers which are greater than its all left elements = 0, 4, 6, 8

### Instance-3

Given Array= [-2, 3, -9, 12, 0, -7].

The numbers which are greater than its all left elements = 3, 12

### Algorithm

Step 1 − Declare an array with some random integer values by static input method.

Step 2 − Take a for loop to iterate all the elements, in which we check whether the present number is greater than its all left elements or not.

Step 3 − If we find that the present number is the greater one from all the left elements to it then print that number and go for next checking.

Step 4 − If we do not get any of greater value in our array then we print as “N/A”.

### Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it-

array.length

where, ‘array’ refers to the array reference.

### Multiple Approaches

We have provided the solution in different approaches.

• Without Using User Defined Method

• With Using User Defined Method

Let’s see the program along with its output one by one.

## Approach-1: Without Using User-Defined Method

In this approach, we declare an array with some random integer values and by using our algorithm we find the present number is greater than its all left elements and print that number as output.

### Example

public class Main {
public static void main (String[ ] args){

//declare the first integer type array and store some random integer values
int inputArray[] = { 1,2,3,8,5};

//if no greater element present in Array
int count = 0;

//output line
System.out.println("Array elements which are greater than its all left element:");

//initiate the loop to find the greater elements
for (int i = inputArray.length - 1; i > 0; i--){
int flag = 0;
for (int j = i - 1; j >= 0; j--){
if (inputArray[i] <= inputArray[j]){
flag = 1;
break;
}
}
if (flag == 0)
System.out.print(inputArray[i] + " ");
count+=1;
}
//if no greater value detected
if(count==0){

//print not available as output
System.out.print(" N/A ");
}
System.out.print("\n");
}
}


### Output

Array elements which are greater than its all left element:
8 3 2


## Approach-2: With Using User Defined Method

In this approach, we declare an array with some random integer values and pass that array and the length of that array as parameters to the user defined method and in the user defined method using the algorithm, find the present number is greater than its all left elements and print that number as output.

### Example

public class Main {
public static void main (String[ ] args){

//declare the array and initialize it with integer values
int inputArray1[] = { 132, 145, 12,-121, 765};

//call the user-defined method and pass the array with its length
greaterThanLeft(inputArray1,inputArray1.length);
}

//user defined method
public static void greaterThanLeft (int[] arr,int n){

//declare an integer value for counting the greater elements
int count = 0;

//output line
System.out.println("Array elements which are greater than its all left element:");

//take a for loop to iterate and find the greater elements
for (int i = n - 1; i > 0; i--){
int flag = 0;
for (int j = i - 1; j >= 0; j--){
if (arr[i] <= arr[j]){
flag = 1;
break;
}
}
if (flag == 0)
System.out.print(arr[i] + " ");
count+=1;
}

//if no greater value detected
if(count==0){

//print not available as output
System.out.print(" N/A ");
}
System.out.print("\n");
}
}


### Output

Array elements which are greater than its all left element:
765 145


In this article, we explored different approaches to find array elements which are greater than all its left elements in Java.