How To Find Top Two Maximum Numbers In a Array Java Program
Program:
Write a program to find top two maximum numbers in a array?
Description:
Write a program to find top two maximum numbers in the
given array. You should not use any sorting functions. You
should iterate the array only once. You should not use any
kind of collections in java.
Code:
package com.java2novice.algos;
public class TwoMaxNumbers {
public void printTwoMaxNumbers(int[] nums){
int maxOne = 0;
int maxTwo = 0;
for(int n:nums){
if(maxOne < n){
maxTwo = maxOne;
maxOne =n;
} else if(maxTwo < n){
maxTwo = n;
}
}
System.out.println("First Max Number: "+maxOne);
System.out.println("Second Max Number: "+maxTwo);
}
public static void main(String a[]){
int num[] = {5,34,78,2,45,1,99,23};
TwoMaxNumbers tmn = new TwoMaxNumbers();
tmn.printTwoMaxNumbers(num);
}
}
Output:
First Max Number: 99
Second Max Number: 78
Program:
Write a program to find top two maximum numbers in a array?
Description:
Write a program to find top two maximum numbers in the
given array. You should not use any sorting functions. You
should iterate the array only once. You should not use any
kind of collections in java.
Code:
package com.java2novice.algos;
public class TwoMaxNumbers {
public void printTwoMaxNumbers(int[] nums){
int maxOne = 0;
int maxTwo = 0;
for(int n:nums){
if(maxOne < n){
maxTwo = maxOne;
maxOne =n;
} else if(maxTwo < n){
maxTwo = n;
}
}
System.out.println("First Max Number: "+maxOne);
System.out.println("Second Max Number: "+maxTwo);
}
public static void main(String a[]){
int num[] = {5,34,78,2,45,1,99,23};
TwoMaxNumbers tmn = new TwoMaxNumbers();
tmn.printTwoMaxNumbers(num);
}
}
Output:
First Max Number: 99
Second Max Number: 78
To find the top two maximum numbers in an array in Java, you can iterate through the array and keep track of the two largest numbers found so far. Here is a sample Java program to achieve this:
public class TopTwoMaxNumbers {
public static void main(String[] args) {
int[] numbers = {3, 5, 1, 4, 9, 7, 2, 8};
findTopTwoMaxNumbers(numbers);
}
public static void findTopTwoMaxNumbers(int[] numbers) {
if (numbers == null || numbers.length < 2) {
System.out.println("Array must have at least two elements");
return;
}
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
for (int number : numbers) {
if (number > max1) {
max2 = max1;
max1 = number;
} else if (number > max2 && number < max1) {
max2 = number;
}
}
System.out.println("The top two maximum numbers are: " + max1 + " and " + max2);
}
}
Explanation
Initialization:
max1andmax2are initialized toInteger.MIN_VALUEto ensure that any number in the array will be larger than these initial values.
Iteration:
- The program iterates through each number in the array.
- If the current number is greater than
max1, it updatesmax2to bemax1(the previous largest number) and then setsmax1to the current number. - If the current number is not greater than
max1but is greater thanmax2, it updatesmax2to the current number.
Output:
- After the loop,
max1andmax2hold the two largest numbers in the array. - The program prints these two numbers.
- After the loop,
Edge Cases
- Array with less than two elements: The program checks if the array has at least two elements and prints an error message if it doesn't.
- Negative numbers and duplicates: The program handles negative numbers and duplicates correctly. If there are duplicates of the maximum number,
max1will hold the maximum number, andmax2will hold the next distinct largest number.
This program is efficient with a time complexity of O(n), where n is the number of elements in the array, as it only requires a single pass through the array.
No comments:
Write comments