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

Answers were Sorted based on User's Feedback



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Post New Answer

More C Interview Questions

#include<stdio.h> int main() { int i=0,j=1,k=2,m,n=0; m=i++&&j++&&k++||n++; printf("%d,%d,%d,%d,%d",i,j,k,m,n); }

12 Answers   Capital IQ, Sasken,


1. Can we use the for loop this way? for(;i>5;) 2. What is the use of pointers? 3. what is array of pointer? 4. What is the difference between file and database? 5. Define data structure?

1 Answers  


What is the use of void pointer and null pointer in c language?

0 Answers  


main() { char ch='356'; Printf("%d",ch); } *OUTPUT*:- -18 *Why?*

1 Answers  


Explain what is the difference between far and near ?

0 Answers  






What is volatile in c language?

2 Answers   HCL, TCS,


any C program contains only one function, it must be a) void () b) main () c) message () d) abc ()

0 Answers  


when user give a number it multiply with 9 without useing '+' and '*' oprator

4 Answers  


Which is best linux os?

0 Answers  


How many levels deep can include files be nested?

0 Answers  


Total of how many functions are available in c?

3 Answers  


writw a program to insert an element in the begning of a doubly linked list

1 Answers  


Categories