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


Please Help Members By Posting Answers For Below Questions

What does volatile do?

560


in any language the sound structure of that language depends on its a) character set, input/output function, its control structures b) character set, library functions, input/output functions its control structures c) character set, library functions, control sturctures d) character set, operators, its control structures

673


write a program that types this pattern: 12345678987654321 12345678 87654321 1234567 7654321 123456 654321 12345 54321 1234 4321 123 321 12 21 1 1

3272


What does c value mean?

616


write a C program: To recognize date of any format even formats like "feb-02-2003","02-february-2003",mm/dd/yy, dd/mm/yy and display it as mm/dd/yy.

3331






What is the best way of making my program efficient?

561


How do you convert a decimal number to its hexa-decimal equivalent.Give a C code to do the same

647


What are different storage class specifiers in c?

611


Write a program to print all permutations of a given string.

638


What is c token?

601


why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???

1510


I have seen function declarations that look like this

593


Explain the use of 'auto' keyword

671


What is spaghetti programming?

664


Why is structure padding done in c?

637