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 the difference between strcpy() and memcpy() function in c programming?

1090


What are the different properties of variable number of arguments?

1165


develop algorithms to add polynomials (i) in one variable

2169


What is the method to save data in stack data structure type?

1089


I need a sort of an approximate strcmp routine?

1032


What is the difference between int main and void main?

1051


How can I write a function that takes a format string and a variable number of arguments?

1058


How to set file pointer to beginning c?

1203


What are the functions to open and close file in c language?

1205


Do you know the difference between malloc() and calloc() function?

1066


Write a code to remove duplicates in a string.

1039


What is binary tree in c?

1105


What does %d do?

1279


Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;

1075


Define C in your own Language.

1085