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

In the previous article, we have seen Java Program to Find all Elements in Array which have at-least Two Greater Elements

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

Java Program to Find all Elements in Array which have at-least Two Smaller 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 smaller elements.

Method-1: Java Program to Find all Elements in Array which have at-least Two Smaller 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 smaller 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 smaller elements
        System.out.print("Elements with at least two smaller elements: ");
        findElewithTwoGreaterElements(arr);
    }

    //findElewithTwoSmallerElements() user defined method 
    //to find elements which have at least 2 greater elements in array
    private static void findElewithTwoSmallerElements(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 smaller than the current element
                //if smaller 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: 4 1 5 3 2
Elements with at least two greater elements: 4 5 3

Method-2: Java Program to Find all Elements in Array which have at-least Two Smaller 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 from arr[2] to arr.length

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 smaller elements
        System.out.print("Elements with at least two smaller elements: ");
        findElement(arr);
    }

    //findElement() method to find all elements which have at least 2 smaller elements
    public static void findElement(int[] arr) 
    {
        //sorting the array using Arrays.sort
        Arrays.sort(arr);
        //pritning all the elements from arr[2] to arr.length
        //as other elements have at least 2 smaller elements except first 2 elements as array is sorted
        for (int i = 2; i < arr.length; i++) 
        {
            System.out.print(arr[i] + " ");
        }
    }
}
Output:

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

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: