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 the difference between strcpy() and memcpy() function in c programming?
What are the different properties of variable number of arguments?
develop algorithms to add polynomials (i) in one variable
What is the method to save data in stack data structure type?
I need a sort of an approximate strcmp routine?
What is the difference between int main and void main?
How can I write a function that takes a format string and a variable number of arguments?
How to set file pointer to beginning c?
What are the functions to open and close file in c language?
Do you know the difference between malloc() and calloc() function?
Write a code to remove duplicates in a string.
What is binary tree in c?
What does %d do?
Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;
Define C in your own Language.