Java Program to Find All the Elements in an Array that are Smaller than All Elements to Their Right

In the previous article, we have seen
Java Program to Find All the Elements in an Array that are Greater than All Elements to Their Right

In this article we will see how to find all the elements in an array that are smaller than all elements to their right by using Java Programming language.

Java Program to Find All the Elements in an Array that are Smaller than All Elements to Their Right

Prerequisite: 

See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.

Let’s see different ways to find all the elements in an array that are smaller than all elements to their right.

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 an array along with array elements.
  • Iterate the array by using for loop.
  • By using brute force method check that current element is smaller than all elements to its right or not.
  • If it is smaller than all elements to its right 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 smaller 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 smaller 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 smaller then break this loop
                //means all elements right to array[i] are not greater
                //again it means array[i] is not smaller than all elements to their right
                if (array[j] < array[i])
                {
                    break;
                }
                //if 'j' reached to last index 
                //that means array[i] is smaller than all elements to its right
                //so print array[i]
                if (j==array.length-1)
                {
                    System.out.print(array[i]+" ");
                }
            }
        }
    }
}
Output:

Original array:
43 78 9 36 29 45 
All the elements that are smaller than all elements to their right: 
9 29

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 smaller than all elements to its right or not.
  • If it is smaller than all elements to its right 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 smaller 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 smaller 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 smaller then break this loop
                //means all elements right to array[i] are not greater
                //again it means array[i] is not smaller than all elements to their right
                if (array[j] < array[i])
                {
                    break;
                }
                //if 'j' reached to last index 
                //that means array[i] is smaller than all elements to its right
                //so print array[i]
                if (j==array.length-1)
                {
                    System.out.print(array[i]+" ");
                }
            }
        }
    }
}
Output:

Enter the number of elements in the array: 6
Enter the elements: 20 60 90 30 40 70
Original array:
20 60 90 30 40 70 
All the elements that are smaller than all elements to their right: 
20 30 40

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 Programs: