Sunday, March 11, 2012

Nth Largest number in List


import java.io.*;

public class NthLargest {

public static void main(String args[])
{
int A[] = new int[]{6,5,2,7,3,8,32,1,43,87,45};
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter number");
try {
int n = Integer.parseInt(reader.readLine());
int res = 0;
for(int i=0;i<A.length;i++)
{
int k = 0;
for(int j=0;j<A.length;j++)
{
if(A[i]<A[j])
++k;
if((k==(n-1))&&(j==A.length-1))
{
res = A[i];
i = A.length;
}
}
}
System.out.println("The "+n+"th largest number is  :"+res);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//This is the supposed nth largest number,here it is third
}
}

No comments:

Post a Comment