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

What is openmp in c?

969


What is scope of variable in c?

991


What is else if ladder?

965


write a program in c language to print your bio-data on the screen by using functions.

6733


When should a type cast not be used?

1005


Explain pointer. What are function pointers in C?

1016


Why do we use return in c?

951


How can I find out the size of a file, prior to reading it in?

1100


What are valid signatures for the Main function?

1170


What does malloc () calloc () realloc () free () do?

1003


What is logical error?

1064


Why is c not oop?

929


Explain what are the advantages and disadvantages of a heap?

1032


How can you return multiple values from a function?

1051


Can we assign string to char pointer?

1042