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
write a program to display all prime numbers
When should we use pointers in a c program?
What’s the special use of UNIONS?
What is the advantage of c?
What is function pointer c?
Explain the ternary tree?
a value that does not change during program execution a) variabe b) argument c) parameter d) none
Write a code to remove duplicates in a string.
What are the 5 elements of structure?
What is context in c?
What is wrong with this code?
write a program to reverse a every alternetive words in a string in a place. EX: Input is "this is the line of text" Output should be "shit is eht line fo text" Please any one tell me code for that.
What is the difference between int main and void main in c?
What is the description for syntax errors?
Explain the use of keyword 'register' with respect to variables.