Find Array Elements Which are Greater Than Its Immediate Left Element?


As per the problem statement, we have an array with some random integer values and we need to find out the numbers which are greater than its immediate left element.

Note - Take an integer array.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

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

The numbers which are greater than its left element = 2, 0, 5

Instance-2

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

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

Instance-3

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

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

Algorithm

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

Step 2 −Take a for loop in which we can check whether the present number is greater than it left element or not.

Step 3 − If we find that the present number is the greater one 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.

  • By Using Static Input Method

  • By Using User Defined Method

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

Approach-1: By Using Static Input 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 left element and print that number as output.

Example

public class Main {
   public static void main (String[ ] args){
      
      //declare an integer type array and store some random integer values
      int inputArray[] = { 14, 16, 3, 5, 2};
      
      //declare an integer variable to store the length of the given Array
      int len = inputArray.length;
      int count = 0;
      
      //output line
      System.out.println("Array elements which are greater than its left element:");
      
      //take the loop to find the greater elements
      for (int i = 1; i < len; i++) {
         
         //check whether the present number is greater than its left element or not
         if (inputArray[i] > inputArray[i-1]){
         
         //print the greater elements if available
            System.out.print (inputArray[i] +" ");
            
            //increament the count value
            //if any greater value found
            count+=1;
         }
      }
      
      //if no greater value detected
      if(count==0){
         
         //print not available as output
         System.out.print(" N/A ");
      }
   }
}

Output

Array elements which are greater than its left element:
16 5

Approach-2: By 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 in our user defined method and in user defined method by using the algorithm, we find the present number is greater than its left element and print that number as output.

Example

public class Main {
   public static void main (String[ ] args){
      //declare two arrays 
      int inputArray1[] = {12, 13, 92,-11, 65};
      int inputArray2[] = {5, 4, 3, 2, 1};
      //call the user-defined method 
      greaterThanLeft(inputArray1,inputArray1.length);
      greaterThanLeft(inputArray2,inputArray2.length);
   }
   
   //user defined method
   public static void greaterThanLeft (int[] arr,int n){
      int count = 0;
      System.out.println("Array elements which are greater than its left element:");

      
      //take a for loop to find the greater elements
      for (int i = 1; i < n; i++) {
         
         //check whether the present number is greater than its left element or not
          if (arr[i] > arr[i-1]){
            
            //print the greater elements if available
            System.out.print (arr[i] +" ");
           
           //increament the count value
            //if any greater value found
            count+=1;
         }
      }
      
      //if no greater value detected
      if(count==0){
         System.out.print(" N/A ");
      }
      System.out.print("\n");
   }
}

Output

Array elements which are greater than its left element:
13 92 65 
Array elements which are greater than its left element:
 N/A

In this article, we explored different approaches to find the array elements which are greater than its left elements by using Java programming language.

Updated on: 01-Mar-2023

858 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements