Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Java Program to find top two maximum number in array
Ans:
In this tutorial, you will learn how to write Java program to find the largest two numbers in a given array.
Below are the approach which we will be follow to write our program:
- In this program first we will take an array and take some elements as an input from users.
- Then we will iterate the loop and then will compare elements till the and during comparison we will find the greatest two elements in a given array.
- You can also follow a approach like first short an array in ascending order and then select top two distinct elements of an array.
How our program will behave?
As we have already seen above our logic to find the first two maximum number of a given array.
Our program will take an array as an input.
And on the basis of inputs it will compare each elements and on the basis of comparison it will print two greatest elements.
Java Program to find two largest number in an array
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array: ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " +n+ " array elements ");
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
for(int i=n-1;i>1;i--){
if(arr[i]!=arr[i-1]){
System.out.println(arr[i] + " and "+arr[i-1]+" is two maximum elements in array");
break;
}
}
}
}
Output:
Also prepare these Java Programs:
- Write a program in Java to print the Fibonacci series using iterative method.
- Palindrome program in Java using Iterative method
- Java program to find largest of three numbers
- Java program to sum the digits of a number using recursion
- Java program to swap two numbers using third variable
- Java Program to check if two strings are anagrams
- Java program to find largest and smallest number in an array
- Java Program to find top two maximum number in array
- Java Program to remove duplicate elements from an array
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program