In the previous article, we have seen Java Program to Delete All the Positive Elements from the Array
In this article we will see how to find all the elements in an array that are greater than all elements to their right by using Java Programming language.
Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Right
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 right.
- By Static Initialization of Array Elements
- By Dynamic Initialization of Array Elements
- By Using Stack
Method-1: Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Right By Static Initialization of Array Elements
Approach:
- Declare one array with elements.
- Iterate the array by using for loop.
- By using brute force method check that current element is greater than all elements to its right or not.
- If it is greater than all elements to its right then print it.
- Finally print the last element also as there is no other element after it.
Program:
import java.util.*; class Main { //driver method public static void main(String[] args) { //declared an array int[] array = { 43, 78, 16, 29, 5 }; //printing the original array System.out.println("Original array:"); for(int i = 0; i < array.length ; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("All the elements that are greater than all elements to their right: "); //calling the user defined method find(array); } //find() method to find all the elements in an array //that are greater than all elements to their right public static void find(int[] array) { for (int i=0; i<array.length; i++) { for (int j=i+1; j<array.length; j++) { //if any element right to array[i] is greater then break this loop //means all elements right to array[i] is not smaller //again it means array[i] is not greater than all elements to their right if (array[j] > array[i]) { break; } //if 'j' reached to last index //that means array[i] is greater than all elements to its right //so print array[i] if (j==array.length-1) { System.out.print(array[i]+" "); } } //printing the last element //as there is no element right to it //so consider it as last greater element if (i==array.length-1) { System.out.print(array[i]+" "); } } } }
Output: Original array: 43 78 16 29 5 All the elements that are greater than all elements to their right: 78 29 5
Method-2: Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Right 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 right or not.
- If it is greater than all elements to its right then print it.
- Finally print the last element also as there is no other element after 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 elemnts 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 right: "); //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 right public static void find(int[] array) { for (int i=0; i<array.length; i++) { for (int j=i+1; j<array.length; j++) { //if any element right to array[i] is greater then break this loop //means all elements right to array[i] is not smaller //again it means array[i] is not greater than all elements to their right if (array[j] > array[i]) { break; } //if 'j' reached to last index //that means array[i] is greater than all elements to its right //so print array[i] if (j==array.length-1) { System.out.print(array[i]+" "); } } //printing the last element //as there is no element right to it //so consider it as last greater element if (i==array.length-1) { System.out.print(array[i]+" "); } } } }
Output: Enter the number of elements in the array: 8 Enter the elements: 90 10 60 30 40 70 50 20 Original array: 90 10 60 30 40 70 50 20 All the elements that are greater than all elements to their right: 90 70 50 20
Method-3: Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Right By Using Stack
Approach:
- Check if the array is empty which means it will not return any output.
- After checking condition in 1, and only if it is false we will declare a Stack of Integer say stack
- Then we will traverse each element of the array using for-each loop
- Inside Loop, check if the current element is greater than the element on the top of the stack if true, we will pop out the top element from the stack.(Because now which is in top is less than current value, if we will keep that in stack so next element will be bigger than that which is not allowed, as all the next elements right to that needs to be smaller so that it can become bigger than all the elements to its right. )
- If the condition is false It will just push the current element into the stack.
- At last, the remaining elements in the stack will be output.
Program:
import java.util.Stack; class Main { //driver method public static void main(String[] args) { //declared an array int[] array = { 43, 78, 16, 29, 5 }; //printing the original array System.out.println("Original array:"); for(int i = 0; i < array.length ; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("All the elements that are greater than all elements to their right: "); //calling the user defined method find(array); } //find() method to find all the elements in an array //that are greater than all elements to their right public static void find(int[] array) { // base condition if (array == null || array.length == 0) { return; } // create an empty stack Stack<Integer> stack = new Stack<>(); // traverse each element of array using for-each loop for (int element: array) { // pop out all the elements that are less than the current element while (!stack.isEmpty() && stack.peek() < element) { stack.pop(); } // push current element into the stack stack.push(element); } // print all elements in the stack while (!stack.isEmpty()) { System.out.print(stack.pop() + " "); } } }
Output: Original array: 43 78 16 29 5 All the elements that are greater than all elements to their right: 5 29 78
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Articles:
- 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 Number of 1’s in an Integer Array
- Java Program to Move An Array Element From One Array Position to Another Position