Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

find the third largest number in an array

// Java program to find third Largest element in an array
class GFG {
 
    static void thirdLargest(int arr[], int arr_size) {
        /* There should be atleast three elements */
        if (arr_size < 3) {
            System.out.printf(" Invalid Input ");
            return;
        }
 
        // Initialize first, second and third Largest element
        int first = arr[0], second = Integer.MIN_VALUE,
                            third = Integer.MIN_VALUE;
 
        // Traverse array elements to find the third Largest
        for (int i = 1; i < arr_size; i++) {
            /* If current element is greater than first,
        then update first, second and third */
            if (arr[i] > first) {
                third = second;
                second = first;
                first = arr[i];
            } /* If arr[i] is in between first and second */
            else if (arr[i] > second) {
                third = second;
                second = arr[i];
            } /* If arr[i] is in between second and third */
            else if (arr[i] > third) {
                third = arr[i];
            }
        }
 
        System.out.printf("The third Largest element is %d
", third);
    }
 
    /* Driver program to test above function */
    public static void main(String []args) {
        int arr[] = {12, 13, 1, 10, 34, 16};
        int n = arr.length;
        thirdLargest(arr, n);
    }
}
//This code is contributed by 29AjayKumar
Comment

find the third largest number in an array

// Java program to find third
// Largest element in an array
// of distinct elements
 
class GFG
{
static void thirdLargest(int arr[],
                         int arr_size)
{
    /* There should be
    atleast three elements */
    if (arr_size < 3)
    {
        System.out.printf(" Invalid Input ");
        return;
    }
 
    // Find first
    // largest element
    int first = arr[0];
    for (int i = 1;
             i < arr_size ; i++)
        if (arr[i] > first)
            first = arr[i];
 
    // Find second
    // largest element
    int second = Integer.MIN_VALUE;
    for (int i = 0;
             i < arr_size ; i++)
        if (arr[i] > second &&
            arr[i] < first)
            second = arr[i];
 
    // Find third
    // largest element
    int third = Integer.MIN_VALUE;
    for (int i = 0;
             i < arr_size ; i++)
        if (arr[i] > third &&
            arr[i] < second)
            third = arr[i];
 
    System.out.printf("The third Largest " +
                  "element is %d
", third);
}
 
// Driver code
public static void main(String []args)
{
    int arr[] = {12, 13, 1,
                 10, 34, 16};
    int n = arr.length;
    thirdLargest(arr, n);
}
}
 
// This code is contributed
// by Smitha
Comment

PREVIOUS NEXT
Code Example
Java :: android snackbar message is behind back button 
Java :: install oracle java ubuntu 20.04 
Java :: java heap sort heapify geeks for geeks 
Java :: t implements comparable 
Java :: java map remove all null values 
Java :: java hashmap increase value by 1 
Java :: seek bar 
Java :: how to upload image from android app to server 
Java :: java list iterator example 
Java :: Could not resolve project :app 
Java :: java exception override message 
Java :: Java 2-dimensional Array 
Java :: determine if a number is factorial in Java 
Java :: qr code spring boot 
Java :: index 1 out of bound for length 1 java 
Java :: exception class implementation in java 
Java :: mergesort java 
Java :: limit decimals java 
Java :: java casting to int 
Java :: Java Expression statements 
Java :: how to get length of 2d array java 
Java :: android studio convert java to kotlin 
Java :: import in java 
Java :: java hashmap 
Java :: java application 
Java :: my canvas java 
Java :: how to use another class in java inside of an action listenrer 
Java :: android activity set action bar options 
Java :: assign a random number in a set without replacement javva 
Java :: how to set credentials for speechClient Java google api 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =