Popular Posts

July 16, 2024

Find Top Two Maximum Numbers In a Array Java Program

 

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

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

  1. Initialization:

    • max1 and max2 are initialized to Integer.MIN_VALUE to ensure that any number in the array will be larger than these initial values.
  2. Iteration:

    • The program iterates through each number in the array.
    • If the current number is greater than max1, it updates max2 to be max1 (the previous largest number) and then sets max1 to the current number.
    • If the current number is not greater than max1 but is greater than max2, it updates max2 to the current number.
  3. Output:

    • After the loop, max1 and max2 hold the two largest numbers in the array.
    • The program prints these two numbers.

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, max1 will hold the maximum number, and max2 will 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