
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Find Array Elements Which are Greater than its Left Elements in Java?
- Finding element greater than its adjacent elements in JavaScript
- C++ Program to count of array elements greater than all elements on its left and at least K elements on its right
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- Python – Sort Matrix by Number of elements greater than its previous element
- Find a string such that every character is lexicographically greater than its immediate next character in Python
- Elements greater than the previous and next element in an Array in C++
- Check which element in a masked array is greater than a given value
- Find smallest element greater than K in Python
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Find all elements in array which have at-least two greater elements in C++
- Mask array elements greater than a given value in Numpy
- Find the number of elements greater than k in a sorted array using C++
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Program to filter all values which are greater than x in an array
