find second largest element in array w/o using sorting
techniques? use onle one for loop.

Answer Posted / abhineet

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=max;

for(int i=1;i<arr.length;i++){
if(max<arr[i]){
scndMax = max;
max = arr[i];
}else if(arr[i]>scndMax || max==scndMax ){
scndMax = arr[i];
}
}
System.out.println("max::"+max);
System.out.println("scndMax::"+scndMax);
}
}

Is This Answer Correct ?    11 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

main(){char *str;scanf("%s",str);printf("%s",str); }The error in the above program is: a) Variable 'str' is not initialised b) Format control for a string is not %s c) Parameter to scanf is passed by value. It should be an address d) none

720


Explain what will the preprocessor do for a program?

593


Write a program which returns the first non repetitive character in the string?

595


What do you mean by scope of a variable in c?

540


Why doesnt that code work?

597






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

572


What are the types of i/o functions?

675


Is void a keyword in c?

570


What is the translation phases used in c language?

625


How is = symbol different from == symbol in c programming?

607


How to check whether string is a palindrome, WITHOUT USING STRING FUNCTIONS?

15486


Can the “if” function be used in comparing strings?

584


What is the difference between array and pointer?

560


What are Macros? What are its advantages and disadvantages?

638


Write a program to swap two numbers without using third variable in c?

611