Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

How can I do serial ("comm") port I/O?

1180


What is ## preprocessor operator in c?

1082


How is a pointer variable declared?

1086


Where register variables are stored in c?

960


Tell me what are bitwise shift operators?

1132


Do you have any idea how to compare array with pointer in c?

1048


Can we change the value of #define in c?

996


What is #define?

1108


What is the heap?

1216


Why doesnt that code work?

1171


Explain what are its uses in c programming?

1055


What do you mean by keywords in c?

1144


Why is sizeof () an operator and not a function?

1028


What are types of preprocessor in c?

1066


How do I get a null pointer in my programs?

1110