Find top two highest numbers in a array java


  • Hi Friends today we will discuss about how to find top two maximum numbers in an array using java program.
  • For this we have written separate function to perform logic
  • findTwoMaxNumbers method takes integer  array as an argument
  • Initially take two variables to find top to numbers and assign to zero.
  • By using for each loop iterating array and compare current value with these values
  • If our value is less than current array value then assign current value to max1 
  • And assign maxone to maxtwo because maxtwo should be second highest.
  • After completion of all iterations maxone will have top value and maxtwo will have second maximum value.
  • Print first maximum and second maximum values.
  • So from main method create array and pass to findTwoMaxNumbers(int [] ar)



package com.javainhouse;


public class FindTopTwo { public void findTwoMaxNumbers(int[] array){ int maxOne = 0; int maxTwo = 0; for(int i:array){ if(maxOne < i){ maxTwo = maxOne; maxOne =i; } else if(maxTwo < i){ maxTwo = i; } } System.out.println("First Maximum Number: "+maxOne); System.out.println("Second Maximum Number: "+maxTwo); } public static void main(String a[]){ int num[] = {4,23,67,1,76,1,98,13}; FindTopTwo obj = new FindTopTwo(); obj.findTwoMaxNumbers(num); obj.findTwoMaxNumbers(new int[]{4,5,6,90,1}); }

Output:




First Maximum Number: 98
Second Maximum Number: 76
First Maximum Number: 90
Second Maximum Number: 6

Equilibrium index of an array

Equilibrium index of an array is an index such that the sum of elements on whose left is equal to sum of elements on right  in java . For example, in an array A:

A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0

3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]

6 is also an equilibrium index, because sum of zero elements is zero, i.e., A[0] + A[1] + A[2] + A[3] + A[4] + A[5]=0

7 is not an equilibrium index, because it is not a valid index of array A.




 public class EquilibriumJavaInHouse
{

public static void main(String[] args) 
    {
int arr[]={-5, 2, 4, 2, -4, 7, -4};
        int i, j;
        int leftsum, rightsum;
        int count=0;
 
          
         int arraylength=arr.length;
        for (i = 0; i < arraylength; ++i) 
        {
            leftsum = 0;  
            rightsum = 0; 
            
            for (j = 0; j < i; j++)
                leftsum += arr[j];
           
            for (j = i + 1; j < arraylength; j++)
                rightsum += arr[j];
           
            if (leftsum == rightsum)
            {
              System.out.println("Equilibirium found at"+i);
              count++;
            }
                
        }
        if(count==0)
        System.out.println("No Equilibirium found ");
        }

}

OutPut:

Equilibrium index of an array

Program to find largest and smallest numbers among N numbers using an array

Hello all , I hope this blog is helping the readers to solve the issue facing for the code in their projects or on learning basis.
Today I am going to write a program on Printing Largest and Smallest among various numbers in Java . All those who are preparing for an interview
or want to enhance their coding skills in Java can go through the other JAVA PROGRAMS on this blog and you can also provide suggestions on
various other programs we can include in our Coding section in this blog in the comments below .

Now there are various of finding the Largest and Smallest number among various numbers in Java .One of the simplest way is to achieve this
through using Arrays . But the Limitation of using an Array for this purpose is that, we need to define a definite length of the array
and also the elements inside it. Since this is the foremost basic program that should be known to developers ,after this we'll also include
the enhanced versions of this program .


Below you can find the basic steps of the program .

1. Create an array with the definite size and elements predefined.
2. Assign first element of an array to two variables  named as largest and smallest.
3. Now using a FOR loop traverse the array .
4. If any  no. found in Array which is larger than the one assigned in variable named as largest the new no. will be assigned to variable largest.
5. If any  no. found in Array which is smaller than the one assigned in variable named as smallest the new no. will be assigned to variable smallest.
The loop will be executed  as many times as length of the array.
6. After the loop has ended ,Print both the numbers .


public class JavaInHouseLSExample{

        public static void main(String[] args) {
             
               
                int numbersArrayArray[] = new int[]{32,43,53,54,32,}; // (1)
             
               
                int smallest = numbersArray[0]; // (2)
                int largest = numbersArray[0]; // (2)
             
                for(int i=1; i< numbersArray.length; i++) //(3)
                {
                        if(numbersArray[i] > largest)
                                largest = numbersArray[i]; //(4)
                        else if (numbersArray[i] < smallest)
                                smallest = numbersArray[i]; //(5)
                     
                }
             
                System.out.println("Largest Number is : " + largest); //(6)
                System.out.println("Smallest Number is : " + smallest);//(6)
        }
}





if you have any questions or suggestions please comment below.