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 are the advantages of the functions?
How do you do dynamic memory allocation in C applications?
What is the use of typedef in structure in c?
Write a code to generate a series where the next element is the sum of last k terms.
What is difference between far and near pointers?
What are the c keywords?
What is difference between main and void main?
What are the types of c language?
What are static variables in c?
What should malloc() do? Return a null pointer or a pointer to 0 bytes?
How can you access memory located at a certain address?
What is the significance of an algorithm to C programming?
What math functions are available for integers? For floating point?
what is the syallabus of computer science students in group- 1?
Why clrscr is used in c?