find second largest element in array w/o using sorting
techniques? use onle one for loop.
Answer Posted / himanshu mertia
package myPackage;
public class BiggestElementInArray {
public static void main(String agrgs[]){
int arr[] = {10,-1,-2,8,-3,-4,-5};
int max = arr[0];
int scndMax=arr[1];
for(int i=1;i<arr.length;i++){
if(arr[i]>max){
scndMax = max;
max = arr[i];
}else if(arr[i]>scndMax){
scndMax = arr[i];
}
}
System.out.println("max::"+max);
if(max != scndMax)
{
System.out.println("scndMax::"+scndMax); }
else { System.out.println("scndMax does not
exist"); }
}
}
this will give output in all conditions..njoy
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
How can I do serial ("comm") port I/O?
What is ## preprocessor operator in c?
How is a pointer variable declared?
Where register variables are stored in c?
Tell me what are bitwise shift operators?
Do you have any idea how to compare array with pointer in c?
Can we change the value of #define in c?
What is #define?
What is the heap?
Why doesnt that code work?
Explain what are its uses in c programming?
What do you mean by keywords in c?
Why is sizeof () an operator and not a function?
What are types of preprocessor in c?
How do I get a null pointer in my programs?