
- 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 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.
- Related Articles
- Find Array Elements Which are Greater Than Its Immediate Left Element?
- C++ Program to count of array elements greater than all elements on its left and at least K elements on its right
- Finding element greater than its adjacent elements in JavaScript
- Find all elements in array which have at-least two greater elements in C++
- Find Elements Greater Than a Given Number In a Subarray in Java
- Mask array elements greater than a given value in Numpy
- Find the number of elements greater than k in a sorted array using C++
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Check Average of Odd Elements or Even Elements are Greater in Java
- Find Duplicate Elements and its Frequency in an Array in Java
- Elements greater than the previous and next element in an Array in C++
- Find Number of Array Elements Smaller than a Given Number in Java
- Python – Sort Matrix by Number of elements greater than its previous element
- Mask array elements greater than or equal to a given value in Numpy
