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


Please Help Members By Posting Answers For Below Questions

What are the advantages of the functions?

609


How do you do dynamic memory allocation in C applications?

634


What is the use of typedef in structure in c?

549


Write a code to generate a series where the next element is the sum of last k terms.

741


What is difference between far and near pointers?

614






What are the c keywords?

752


What is difference between main and void main?

630


What are the types of c language?

561


What are static variables in c?

631


What should malloc() do? Return a null pointer or a pointer to 0 bytes?

628


How can you access memory located at a certain address?

670


What is the significance of an algorithm to C programming?

598


What math functions are available for integers? For floating point?

627


what is the syallabus of computer science students in group- 1?

1846


Why clrscr is used in c?

590