Java Program to Find all Elements in Array which have at-least Two Greater Elements

In the previous article, we have seen Java Program to Delete All the Odd Elements from the Array of Integers

In this article we are going to see how to find all elements in array which have at-least two greater elements using Java programming language.

Java Program to Find all Elements in Array which have at-least Two Greater Elements

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find all elements in array which have at-least two greater elements.

Method-1: Java Program to Find all Elements in Array which have at-least Two Greater Elements By Using Iterative Approach

Approach:

  • Create scanner class object.
  • Ask user for the length of the array.
  • Initialize the array with given size.
  • Iterate over the array using two nested loops.
  • Initialize counter variable to 0 inside the outer loop and outside the inner loop.
  • Each time a greater element is found increment counter by one.
  • After the loop is exhausted check if the counter >= 2, print the value.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        
        // call method to find two greater elements
        System.out.print("Elements with at least two greater elements: ");
        findElewithTwoGreaterElements(arr);
    }

    //findElewithTwoGreaterElements() user defined method 
    //to find elements which have at least 2 greater elements in array
    private static void findElewithTwoGreaterElements(int[] arr) 
    {
        //iterating the array to cover each element
        for (int i = 0; i < arr.length; i++) 
        {
            int cnt = 0;
            //iterating the array to compare with current element
            for (int j = 0; j < arr.length; j++) 
            {
                //checking if other element is greater than the current element
                //if greater then incrementing 'cnt' value
                if (arr[j] > arr[i]) 
                {
                    cnt++;
                }
            }
            
            //if 'cnt' value is greater than equal to 2 then it prints that element
            if (cnt >= 2) 
            {
                System.out.print(arr[i] + " ");
            }
        }

    }
}

Output:

Enter the size of array: 5
Enter array elements: 9 4 6 2 8
Elements with at least two greater elements: 4 6 2

Method-2: Java Program to Find all Elements in Array which have at-least Two Greater Elements By Using Sorting Technique

Approach:

  • Create scanner class object.
  • Ask user for the length of the array.
  • Initialize the array with given size.
  • Iterate over the array using two nested loops.
  • Sort the array.
  • Print all the values up to arr.length–2.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        // call method to find two greater elements
        System.out.print("Elements with at least two greater elements: ");
        findElement(arr);
    }

    //findElement() method to find all elements which have at least 2 greater elements
    public static void findElement(int[] arr) 
    {
        //sorting the array using Arrays.sort
        Arrays.sort(arr);
        System.out.print("Element with two greater elements are: ");
        //pritning all the elements upto arr.length-2
        //as other elements have at least 2 greater elements except last 2 elements as array is sorted
        for (int i = 0; i < arr.length-2; i++) 
        {
            System.out.print(arr[i] + " ");
        }
    }
}

Output:

Enter the size of array: 6
Enter array elements: 70 10 30 90 20 50
Elements with at least two greater elements: Element with two greater elements are: 10 20 30 50

Method-3: Java Program to Find all Elements in Array which have at-least Two Greater Elements By Finding Two Largest Element of The Array

Approach:

  • Create scanner class object.
  • Ask user for the length of the array.
  • Initialize the array with given size.
  • Initialize maxi = Integer.MIN_VALUE and maxi2 = Integer.MIN_VALUE.
  • Iterate through the array and find largest(maxi) and second largest element(maxi2).
  • Print all the values less than maxi2

Program:

import java.util.Arrays;
import java.util.Scanner;

import javax.swing.plaf.synth.SynthSpinnerUI;

public class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        // call method to find two greater elements
        System.out.print("Elements with at least two greater elements: ");
        findElements(arr);
    }

    public static void findElements(int arr[]) 
    {
        int maxi = Integer.MIN_VALUE;
        int maxi2 = Integer.MIN_VALUE;

        for (int i = 0; i < arr.length; i++) 
        {
            // If current element is greater
            // than maxi then update both
            // maxi and maxi2
            if (arr[i] > maxi) 
            {
                maxi2 = maxi;
                maxi = arr[i];
            }

            // if arr[i] is in between maxi and maxi2 then update maxi2

            else if (arr[i] > maxi2)
                maxi2 = arr[i];
        }

        for (int i = 0; i < arr.length; i++) 
        {
            if (arr[i] < maxi2) 
            {
                System.out.print(arr[i] + " ");
            }
        }
    }
}

Output:

Enter the size of array: 5
Enter array elements: 50 20 10 40 30
Elements with at least two greater elements: 10 30

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: