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 Left
In this article we will see how to find all the elements in an array that are smaller than all elements to their left by using Java Programming language.
Java Program to Find All the Elements in an Array that are Smaller 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 smaller than all elements to their left.
Method-1: Java Program to Find All the Elements in an Array that are Smaller 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 smaller than all elements to its left or not.
- If it is smaller 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 smaller 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 smaller 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 smaller then break this loop //means all elements left to array[i] are not greater //again it means array[i] is not smaller than all elements to their left if (array[j] < array[i]) { break; } //if 'j' reached to first index //that means array[i] is smaller 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 smaller than all elements to their left: 9
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 smaller than all elements to its left or not.
- If it is smaller 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 smaller 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 smaller 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 smaller then break this loop //means all elements left to array[i] are not greater //again it means array[i] is not smaller than all elements to their left if (array[j] < array[i]) { break; } //if 'j' reached to last index //that means array[i] is smaller 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 20 10 80 60 70 90 40 Original array: 30 20 10 80 60 70 90 40 All the elements that are smaller than all elements to their left: 20 10
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs:
- 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
- Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller