find second largest element in array w/o using sorting
techniques? use onle one for loop.
Answers were Sorted based on User's Feedback
Answer / ranjeet
public static void main(String[] args) {
int array[]={13,12,34,56,73,21,232,234,235,240};
int max ,secndmax;
max = secndmax= array[0];
System.out.println("Initial value is "+ secndmax);
for (int i=1;i<array.length;i++){
if (array[i]>max ){
secndmax=max;
max=array[i];
}else if(array[i]>secndmax){
secndmax = array[i];
}
}
System.out.println("Max element is "+ max);
System.out.println("Second Max element is "+
secndmax);
}
Is This Answer Correct ? | 67 Yes | 32 No |
Answer / rajni kant
public class Findarray {
public static void main(String[] args) {
int array[]={250,12,34,56,73,260,232,234,235,240};
int max ,secndmax;
max = array[0];
secndmax=0;// assign it 0 not by array[0]as initial value
System.out.println("Initial value is "+ max);
for (int i=1;i<array.length;i++){
if (array[i]>max ){
secndmax=max;
max=array[i];
}else if(array[i]>secndmax){
secndmax = array[i];
}
}
System.out.println("Max element is "+ max);
System.out.println("Second Max element is "+
secndmax);
}
}
Is This Answer Correct ? | 32 Yes | 11 No |
Answer / maxerp
int secondLargestNumber(int a[],int numberOfValues)
{
int largest=secondLargest=a[0];
int i;
for(i=1;i<numberOfValues;i++)
{
if(a[i]>largest)
{
secondLargest=largest;
largest=a[i];
}
if(a[i]>secondLargest && a[i]<largest)
secondLargest=a[i];
}
return secondLargest;
}
Is This Answer Correct ? | 29 Yes | 14 No |
Answer / tata indicom
What if the first element which you are assigning to max and
secondmax is itself the largest number in array.....
In such a case you will not be able to find second largest in
the array.....
Is This Answer Correct ? | 9 Yes | 4 No |
Answer / athul
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,i,big,big2;
cout<<"Enter the limit\n";
cin>>n;
cout<<"Enter the arrays\n";
for(i=0;i<n;i++)
{
cin>>a[i];
big=a[0];
}
for(i=0;i<n;i++)
{
if(big<a[i])
{
big2=big;
big=a[i];
}
else if(big2<a[i])
{
big2=a[i];
}
}
cout<<"The second biggest number is "<<big2<<endl;
getch();
}
Is This Answer Correct ? | 6 Yes | 2 No |
Answer / 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 |
Answer / 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 |
Answer / anand
Initializing second_largest to max negative number will ensure for all (+ve and -ve range of values).
int secondLargestNumber(int arr[],int numberOfValues)
{
int largest=arr[0];
int second_largest= -(2^(sizeof(int)*8 -1));
int i;
for(i=1;i<numberOfValues;i++)
{
if(a[i]>largest)
{
secondLargest=largest;
largest=a[i];
}
if(a[i]>secondLargest && a[i]<largest)
secondLargest=a[i];
}
return secondLargest;
}
Is This Answer Correct ? | 4 Yes | 1 No |
Answer / alam cse-35 bangladesh univers
#include<iostream>
using namespace std;
int main()
{
int i,a[]={121,104,105,205,6,25,80,77,120},max=0,second_max=0;
for(i=0;i<=8;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
for(i=0;i<=8;i++)
{
if(a[i]!=max)
{
if(a[i]>second_max)
{
second_max=a[i];
}
}
}
cout<<"Second Highest Value:"<<second_max<<endl;
cout<<"Maximum Value:"<<max;
return 0;
}
Is This Answer Correct ? | 4 Yes | 2 No |
Answer / paresh thorat
package pkg;
public class Number {
public static void main(String [] args){
//int arr[]={6,4,5,2};
int arr[]={1,2,3,4,5};
int max,min=0,i;
max=arr[0];
try {
for(i=1;i<arr.length;i++){
System.out.println("Loop start");
if(arr[i]>max){
min=max;
max=arr[i];
System.out.println("Large->array "+max+"->"+arr[i]);
}
System.out.println("min-> array "+min+"->"+arr[i]);
if(max>arr[i] && arr[i]>min){
System.out.println("I am in second if loop");
min = arr[i];
System.out.println("Now min is "+min);
}
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Second Large"+min);
}
}
Is This Answer Correct ? | 5 Yes | 4 No |
Difference between macros and inline functions? Can a function be forced as inline?
0 Answers HAL, Honeywell, Zomato,
Print the foll in C...eg when n=5 the o/p must b + + + + + + + + + + + + + + + + +
why return type of main is not necessary in linux
what is the difference between strcpy() and memcpy() function?
What is conio h in c?
main() { int ptr[] = {1,2,23,6,5,6}; printf("%d",&ptr[3]-&ptr[0]); }
1.int a=10; 2.int b=20; 3. //write here 4.b=30; Write code at line 3 so that when the value of b is changed variable a should automatically change with same value as b. 5.
what is the associativity of bitwise OR operator?
int array[]={1,2,3,4,5,6,7,8}; #define SIZE (sizeof(array)/sizeof(int)) main() { if(-1<=SIZE) printf("1"); else printf("2"); }
Why cant I open a file by its explicit path?
What is restrict keyword in c?
Write a program in C for showing working of different logical operator in C. Your program should guide users with proper message/menu on the console.