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
What is openmp in c?
What is scope of variable in c?
What is else if ladder?
write a program in c language to print your bio-data on the screen by using functions.
When should a type cast not be used?
Explain pointer. What are function pointers in C?
Why do we use return in c?
How can I find out the size of a file, prior to reading it in?
What are valid signatures for the Main function?
What does malloc () calloc () realloc () free () do?
What is logical error?
Why is c not oop?
Explain what are the advantages and disadvantages of a heap?
How can you return multiple values from a function?
Can we assign string to char pointer?