Tuesday, 15 May 2018




Write a program to find top three 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.
package govind;
public class FindMaximumThreeNumber {
           
            public void printThreeMax(int[] x)
            {
                        int max1=Integer.MIN_VALUE;
                        int max2=Integer.MIN_VALUE;
                        int max3=Integer.MIN_VALUE;
                        for(int y:x)
                        {
                                    if(max1<y)
                                    {
                                                max3=max2;
                                                max2=max1;
                                                max1=y;
                                    }
                                    else if(max2<y)
                                    {
                                                max3=max2;
                                                max2=y;
                                    }
                                    else if(max3<y)
                                    {
                                                max3=y;
                                    }
                                   
                        }
                        System.out.println("First Maximum Number="+max1);
        System.out.println("Second Maximum Number="+max2);
                System.out.println("Third Maximum Number="+max3);
                       
                       
                       
            }
            public static void main(String[] args) {
                        int m[]= {2,10,-9,-6,80,56,78,110,76,34};
                        FindMaximumThreeNumber fmt=new FindMaximumThreeNumber();
                        fmt.printThreeMax(m);
            }

}

o/p:---------------
First Maximum Number=110
Second Maximum Number=80
Third Maximum Number=78

Write a program to find top three minimum 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.

package govind;

public class FindThreeMinimum {
                public void printThreeMin(int[] x)
                {
                                int min1=Integer.MAX_VALUE;
                                int min2=Integer.MAX_VALUE;
                                int min3=Integer.MAX_VALUE;
                                for(int y:x)
                                {
                                                if(min1>y)
                                                {
                                                                min3=min2;
                                                                min2=min1;
                                                                min1=y;
                                                }
                                                else if(min2>y)
                                                {
                                                                min3=min2;
                                                                min2=y;
                                                }
                                                else if(min3>y)
                                                {
                                                                min3=y;
                                                }
                                               
                                }
                                System.out.println("First Minimum Number="+min1);
        System.out.println("Second Minimum Number="+min2);
                    System.out.println("Third Minimum Number="+min3);
                               
                               
                               
                }
                public static void main(String[] args) {
                                int m[]= {2,10,-9,-6,80,56,78,110,76,34};
                                FindThreeMinimum fmt=new FindThreeMinimum();
                                fmt.printThreeMin(m);
                }

}

o/p:-------------------------
First Minimum Number=-9
Second Minimum Number=-6
Third Minimum Number=2

No comments:

Post a Comment