find second largest element in array w/o using sorting
techniques? use onle one for loop.
Answer Posted / rajni kant
public class Findarray {
public static void main(String[] args) {
int array[]={250,12,34,56,73,260,232,234,235,240};
int max ,secndmax;
max = array[0];
secndmax=0;// assign it 0 not by array[0]as initial value
System.out.println("Initial value is "+ max);
for (int i=1;i<array.length;i++){
if (array[i]>max ){
secndmax=max;
max=array[i];
}else if(array[i]>secndmax){
secndmax = array[i];
}
}
System.out.println("Max element is "+ max);
System.out.println("Second Max element is "+
secndmax);
}
}
| Is This Answer Correct ? | 32 Yes | 11 No |
Post New Answer View All Answers
Explain what are compound statements?
What is a rvalue?
What are loops in c?
What are the types of pointers?
What are the valid places to have keyword “break”?
Differentiate between a for loop and a while loop? What are it uses?
main() { printf("hello"); fork(); }
What does main () mean in c?
What are the types of type qualifiers in c?
What is the behavioral difference when include header file in double quotes (“”) and angular braces (<>)?
Hai what is the different types of versions and their differences
What is a function in c?
Is c a great language, or what?
Can you write the algorithm for Queue?
What is the best way of making my program efficient?