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
Why pointers are used in c?
What is a pointer in c?
If null and 0 are equivalent as null pointer constants, which should I use?
What does %p mean?
A c program to display count values from 0 to 100 and flash each digit for a secong.reset the counter after it reaches 100.use for loop,. pls guys hepl me.. :(
What are categories used for in c?
write a c program to find the sum of five entered numbers using an array named number
When should the const modifier be used?
How can you restore a redirected standard stream?
What does double pointer mean in c?
Why do we use int main?
void main(int n) { if(n==0) return; main(--n); printf("%d ",n); getch(); } how it work and what will be its output...............it any one know ans plz reply
Who invented b language?
Which is better oop or procedural?
What is the acronym for ansi?