In the previous article, we have seen Java Program to Find All the Elements in an Array that are Smaller than All Elements to Their Right
In this article we will see how to find all the elements in an array that are greater than all elements to their left by using Java Programming language.
Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Left
Prerequisite:
See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.
- How to Declare an Array in Java?
- How to Instantiate an Array in Java?
- How to Initialize an Array in Java?
Let’s see different ways to find all the elements in an array that are greater than all elements to their left.
Method-1: Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Left By Static Initialization of Array Elements
Approach:
- Declare an array along with array elements.
- Iterate the array by using for loop.
- By using brute force method check that current element is greater than all elements to its left or not.
- If it is greater than all elements to its left then print it.
Program:
import java.util.*; class Main { //driver method public static void main(String[] args) { int arr[]={43,78,9,36,29,45}; //printing the original array System.out.println("Original array:"); for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); System.out.println("All the elements that are greater than all elements to their left: "); //calling the user defined method find(arr); } //find() method to find all the elements in an array //that are greater than all elements to their left public static void find(int[] array) { for (int i=0; i<array.length; i++) { for (int j=i-1; j>=0; j--) { //if any element left to array[i] is greater then break this loop //means all elements left to array[i] are not smaller //again it means array[i] is not greater than all elements to their left if (array[j] > array[i]) { break; } //if 'j' reached to first index //that means array[i] is greater than all elements to its left //so print array[i] if (j==0) { System.out.print(array[i]+" "); } } } } }
Output: Original array: 43 78 9 36 29 45 All the elements that are greater than all elements to their left: 78
Method-2: Java Program to Find All the Elements in an Array that are Smaller than All Elements to Their Left By Dynamic Initialization of Array Elements
Approach:
- Take input of an array.
- Iterate the array by using for loop.
- By using brute force method check that current element is greater than all elements to its left or not.
- If it is greater than all elements to its left then print it.
Program:
import java.util.*; class Main { //driver method public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); int arr[] = new int[num]; System.out.print("Enter the elements: "); //taking input of array elements for (int i = 0; i < num; i++) { arr[i] = sc.nextInt(); } //printing the original array System.out.println("Original array:"); for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); System.out.println("All the elements that are greater than all elements to their left: "); //calling the user defined method find(arr); } //find() method to find all the elements in an array //that are greater than all elements to their left public static void find(int[] array) { for (int i=0; i<array.length; i++) { for (int j=i-1; j>=0; j--) { //if any element left to array[i] is greater then break this loop //means all elements left to array[i] are not smaller //again it means array[i] is not greaterr than all elements to their left if (array[j] > array[i]) { break; } //if 'j' reached to last index //that means array[i] is greater than all elements to its left //so print array[i] if (j==0) { System.out.print(array[i]+" "); } } } } }
Output: Enter the number of elements in the array: 8 Enter the elements: 30 50 80 10 90 60 40 70 Original array: 30 50 80 10 90 60 40 70 All the elements that are greater than all elements to their left: 50 80 90
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Related Java Programs:
- Java Program to Find All the Elements in an Array that are Smaller than All Elements to Their Left
- Java Program to Form Two Numbers (of 2 digit) with Maximum Sum Using Array Elements
- Java Program to Form Two Numbers (of 2 digit) with Minimum Sum Using Array Elements
- Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater