Java Program to Find the Largest Two Numbers in a Given Array

Spread the love

This is a Java Program to Find the Largest Two Numbers in a Given Array.Enter size of array and then enter all the elements of that array. Now we first sort the array in decreasing order using double for loops and hence get the first two elements as output.

Here is the source code of the Java Program to Find the Largest Two Numbers in a Given Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;
public class Largest_Numbers
{
public static void main(String[] args)
{
int n, l1, l2, temp;
Scanner s = new Scanner(System.in);
System.out.print(“Enter no. of elements you want in array:”);
n = s.nextInt();
if(n > 1)
{
int a[] = new int[n];
System.out.println(“Enter all the elements:”);
for(int i = 0; i < n ; i++)
{
a[i] = s.nextInt();
}
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println(“Largest two numbers are:”+a[0]+” and “+a[1]);
}
else
{
System.out.println(“Enter number greater than 1”);
}
}
}
Output:

$ javac Largest_Numbers.java
$ java Largest_Numbers

Enter no. of elements you want in array:8
Enter all the elements:
5
3
7
9
2
3
9
10
Largest two numbers are:10 and 9


Find the largest three elements in an array

Given an array with all distinct elements, find the largest three elements. Expected time complexity is O(n) and extra space is O(1).

Examples :

Input: arr[] = {10, 4, 3, 50, 23, 90}
Output: 90, 50, 23

Below is algorithm:

1) Initialize the largest three elements as minus infinite.
    first = second = third = -∞

2) Iterate through all elements of array.
   a) Let current array element be x.
   b) If (x > first)
      {
          // This order of assignment is important
          third = second
          second = first
          first = x   
       }
   c)  Else if (x > second)
      {
          third = second
          second = x 
      }
   d)  Else if (x > third)
      {
          third = x  
      }

3) Print first, second and third.

Below is the implementation of above algorithm.

// Java code to find largest three elements
// in an array

class PrintLargest
{
/* Function to print three largest elements */
static void print3largest(int arr[], int arr_size)
{
int i, first, second, third;

/* There should be atleast three elements */
if (arr_size < 3)
{
System.out.print(” Invalid Input “);
return;
}

third = first = second = Integer.MIN_VALUE;
for (i = 0; i < arr_size ; i ++)
{
/* If current element is greater than
first*/
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i];
}

/* If arr[i] is in between first and
second then update second */
else if (arr[i] > second)
{
third = second;
second = arr[i];
}

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

System.out.println(“Three largest elements are ” +
first + ” ” + second + ” ” + third);
}

/* Driver program to test above function*/
public static void main (String[] args)
{
int arr[] = {12, 13, 1, 10, 34, 1};
int n = arr.length;
print3largest(arr, n);
}
}
Output :

Three largest elements are 34 13 12

Another approach:An efficient way to solve this problem is to use any O(nLogn)sorting algorithm & simply returning the last 3 largest elements .