find second largest element in array w/o using sorting
techniques? use onle one for loop.
Answer Posted / paresh thorat
package pkg;
public class Number {
public static void main(String [] args){
//int arr[]={6,4,5,2};
int arr[]={1,2,3,4,5};
int max,min=0,i;
max=arr[0];
try {
for(i=1;i<arr.length;i++){
System.out.println("Loop start");
if(arr[i]>max){
min=max;
max=arr[i];
System.out.println("Large->array "+max+"->"+arr[i]);
}
System.out.println("min-> array "+min+"->"+arr[i]);
if(max>arr[i] && arr[i]>min){
System.out.println("I am in second if loop");
min = arr[i];
System.out.println("Now min is "+min);
}
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Second Large"+min);
}
}
Is This Answer Correct ? | 5 Yes | 4 No |
Post New Answer View All Answers
What is #define?
What is the difference between far and near in c?
I have a varargs function which accepts a float parameter?
What is union and structure in c?
Explain logical errors? Compare with syntax errors.
Why c is faster than c++?
write a program to display all prime numbers
What are the differences between new and malloc in C?
What is a null pointer in c?
What does %p mean c?
How can you determine the maximum value that a numeric variable can hold?
What is the difference between far and near ?
Are there namespaces in c?
Tell me what is the purpose of 'register' keyword in c language?
Explain the use of fflush() function?