In the previous article, we have seen Java Program to Form Two Numbers (of 2 digit) with Minimum Sum Using Array Elements
In this article, we will how to find the index of array element, where all the elements before it are smaller and all the elements after it are greater by using Java programming language.
Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater
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 index of array element, where all the elements before it are smaller and all the elements after it are greater.
Method-1: Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater By Static Initialization of Array Elements
Approach:
- Declare an array with array elements as integers.
- Then call user defined method
find()
to find index of array element, where all the elements before it are smaller and all the elements after it are greater. - Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are greater or not by using one for loop and check whether all elements left to it are smaller or not by using another for loop.
- If condition satisfies then print that element with its index.
Program:
import java.util.*; class Main { //driver method public static void main(String[] args) { //declared an array int[] array = { 7,24,32,95,88,82 }; //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(); //finding index of array elements //where all elements before it are smaller //and all elements after it are greater //by calling user defined method find() System.out.println("Array elements left to whose all are smaller and right to whose all are greater: "); int count=find(array); if(count==0) { System.out.print("No such element present in array"); } } //find() method public static int find(int[] array) { boolean left=false; boolean right=false; int count=0; int last=array.length; //iterating the array elements one by one for (int i=0; i<last-1; i++) { //checking all elements to current element's right are greater or not for(int j=i+1; j<array.length; j++) { //if right element is greater than current element i.e array[i] then assign 'true' to right variable //else assign 'false' to right variable and break the loop if(array[i]<array[j]) { right=true; } else { right=false; break; } } //checking all elements to current element's left are smaller or not for(int k=i-1; k>=0; k--) { //if left element is smaller than current elementy i.e array[i] then assign 'true' to left variable //else assign 'false' to left variable and break the loop if(array[i]>array[k]) { left=true; } else { left=false; break; } } //if left value is 'true' and right value is 'true' //then left to array[i] all elements are smaller //and right to array[i] all elements are greater //so print that element with it's index if(left==true && right==true) { System.out.println("Element is "+array[i]+" and index is "+i); count++; } } return count; } }
Output: Original array: 7 24 32 95 88 82 Array elements left to whose all are smaller and right to whose all are greater: Element is 24 and index is 1 Element is 32 and index is 2
Method-2: Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater By Dynamic Initialization of Array Elements
Approach:
- Declare an array and take integer array elements as user input.
- Then call user defined method
find()
to find index of array element, where all the elements before it are smaller and all the elements after it are greater. - Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are greater or not by using one for loop and check whether all elements left to it are smaller or not by using another for loop.
- If condition satisfies then print that element with its index.
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 array[] = new int[num]; System.out.print("Enter the elements: "); //taking input of array elemnts for (int i = 0; i < num; i++) { array[i] = sc.nextInt(); } //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(); //finding index of array elements //where all elements before it are smaller //and all elements after it are greater //by calling user defined method find() System.out.println("Array elements left to whose all are smaller and right to whose all are greater: "); int count=find(array); if(count==0) { System.out.print("No such element present in array"); } } //find() method public static int find(int[] array) { boolean left=false; boolean right=false; int count=0; int last=array.length; //iterating the array elements one by one for (int i=0; i<last-1; i++) { //checking all elements to current element's right are greater or not for(int j=i+1; j<array.length; j++) { //if right element is greater than current element i.e array[i] then assign 'true' to right variable //else assign 'false' to right variable and break the loop if(array[i]<array[j]) { right=true; } else { right=false; break; } } //checking all elements to current element's left are smaller or not for(int k=i-1; k>=0; k--) { //if left element is smaller than current element i.e array[i] then assign 'true' to left variable //else assign 'false' to left variable and break the loop if(array[i]>array[k]) { left=true; } else { left=false; break; } } //if left value is 'true' and right value is 'true' //then left to array[i] all elements are smaller //and right to array[i] all elements are greater //so print that element with it's index if(left==true && right==true) { System.out.println("Element is "+array[i]+" and index is "+i); count++; } } return count; } }
Output: Case-1 Enter the number of elements in the array: 6 Enter the elements: 10 20 30 50 40 60 Original array: 10 20 30 50 40 60 Array elements left to whose all are smaller and right to whose all are greater: Element is 20 and index is 1 Element is 30 and index is 2 Case-2 Enter the number of elements in the array: 6 Enter the elements: 9 5 2 7 6 1 Original array: 9 5 2 7 6 1 Array elements left to whose all are smaller and right to whose all are greater: No such element present in array
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Articles:
- Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller
- 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
- Java Program to Find Sum of Elements of an Array
- Java Program to Find Average of all Array Elements