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 size of a union variable?

596


What is volatile variable in c with example?

581


What is the difference between variable declaration and variable definition in c?

562


write a program that declares an array of 30 elements named "income" in the main functions. then cal and pass the array to a programmer-defined function named "getIncome" within the "getIncome" function, ask the user for annual income of 30 employees. then calculate and print total income on the screen using the following function: "void getIncome ( ai []);

1838


What are the different file extensions involved when programming in C?

749






What are type modifiers in c?

615


#include main() { int *p, *c, i; i = 5; p = (int*) (malloc(sizeof(i))); printf(" %d",*p); *p = 10; printf(" %d %d",i,*p); c = (int*) calloc(2); printf(" %d ",*c); }

622


What does stand for?

590


What is oops c?

602


Given an array of 1s and 0s arrange the 1s together and 0s together in a single scan of the array. Optimize the boundary conditions?

972


How do I swap bytes?

626


What is a function in c?

567


Badboy is defined who has ALL the following properties: Does not have a girlfriend and is not married. He is not more than 23 years old. The middle name should be "Singh" The last name should have more than 4 characters. The character 'a' should appear in the last name at least two times. The name of one of his brothers should be "Ram" Write a method: boolean isBadBoy(boolean hasGirlFriend , boolean isMarried, int age , String middleName , String lastName , String[] brotherName); isHaveGirlFriend is true if the person has a girlfriend isMarried is true if the person is married age is the age of the person middleName is the middle name of the person lastName is the last name of the person brotherName is the array of the names of his brothers

1420


The __________ attribute is used to announce variables based on definitions of columns in a table?

667


How do you define CONSTANT in C?

647