@Arpit

i think finding max & second max does n't reuire to mach time as u
have told  n + log(n) -2 step
check out my solution if i am wrong plz xplain me n + log(n) -2 step
required to solve this problem

public class array
{
        public void getMax( double ar[] )
        {
                double max1 = ar[0]; // Assume the first
                double max2 = ar[0]; // element in the array

                int ZERO = 0; // Variable to store inside it the index of the 
max
value to set it to zero.

                for( int i = 0; i < ar.length; i++ )
                {
                        if( ar[i] >= max1)
                        {
                                max1 = ar[i];
                                ZERO = i;
                        }
                }

                ar[ZERO] = 0; // Set the index contains the 1st max to ZERO.

           //remove elemmn from reset length of array i..e. shrink
array


                for( int j = 0; j < ar.length; j++ )
                {
                        if( ar[j] >= max2 )
                        {
                                max2 = ar[j];
                                ZERO = j;
                        }
                }



                System.out.println("The 1st maximum element in the array is: " +
max1 + ", the 2nd is: " + max2);
        }

        public static void main(String[] args)
        {
                // Creating an object from the class Array to be able to use its
methods.
                array array = new array();
                // Creating an array of type double.
                double a[] = {2.2, 3.4, 5.5, 5.5, 6.6, 5.6};

                array.getMax( a ); // Calling the method that'll find the 1st 
max,
2nd max, and 3rd max.
        }

}

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to