In the previous article, we have seen Java Program to Find the Index of an Element Before Which All elements are Smaller and After Which All Elements are Greater
In this article, we will how to find the index of array element, where all the elements before it are greater and all the elements after it are smaller by using Java programming language.
Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller
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 greater and all the elements after it are smaller.
Method-1: Java Program to Find the Index of an Element Before Which All elements are Greater and After Which All Elements are Smaller 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 greater and all the elements after it are smaller. - Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are smaller or not by using one for loop and check whether all elements left to it are greater 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 = { 57,44,32,15,28,18 }; //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 greater //and all elements after it are smaller //by calling user defined method find() System.out.println("Array elements left to whose all are greater and right to whose all are smaller: "); 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 smaller or not for(int j=i+1; j<array.length; j++) { //if right element is smaller 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 greater or not for(int k=i-1; k>=0; k--) { //if left element is greater 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 greater //and right to array[i] all elements are smaller //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: 57 44 32 15 28 18 Array elements left to whose all are greater and right to whose all are smaller: Element is 44 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 Greater and After Which All Elements are Smaller 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 greater and all the elements after it are smaller. - Inside method iterate array elements one by one by using one for loop and check whether all elements right to it are smaller or not by using one for loop and check whether all elements left to it are greater 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 greater //and all elements after it are smaller //by calling user defined method find() System.out.println("Array elements left to whose all are greater and right to whose all are smaller: "); 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 smaller or not for(int j=i+1; j<array.length; j++) { //if right element is smaller 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 greater or not for(int k=i-1; k>=0; k--) { //if left element is greater 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 greater //and right to array[i] all elements are smaller //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-2 Enter the number of elements in the array: 8 Enter the elements: 80 60 50 20 30 10 40 25 Original array: 80 60 50 20 30 10 40 25 Array elements left to whose all are greater and right to whose all are smaller: Element is 60 and index is 1 Element is 50 and index is 2 Case-1 Enter the number of elements in the array: 8 Enter the elements: 8 4 6 2 1 5 3 7 Original array: 8 4 6 2 1 5 3 7 Array elements left to whose all are greater and right to whose all are smaller: No such element present in array
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: