question1

question1

1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.

if the input array is return
{1, 2, 3, 4} =3
{{4, 1, 2, 3}} =3
{1, 1, 2, 2} =1
{1, 1} =-1
{1} =-1
{} =-1
View Answers

July 9, 2010 at 10:35 AM

Hi Friend,

Try the following code:

import java.util.*;
public class SecondLargestNumber{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter 5 integers: ");
int secondlargest= -1;
int largest = -1;
int numbers[] =new int[5];
for (int i=0;i < numbers.length;i++){
numbers[i]=input.nextInt();
if(largest < numbers[i] ){
secondlargest = largest;
largest = numbers[i];
}
if(secondlargest < numbers[i] && largest != numbers[i] )
secondlargest = numbers[i];
}
System.out.println("Second Largest number is: "+secondlargest);
}
}

Thanks









Related Tutorials/Questions & Answers:
question1 - Java Beginners
question1  1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest. if the input array is return {1, 2, 3, 4} =3 {{4
core java - Java Beginners
core java  question1:- what is default members?  Hi Friend, If there is no access modifiers(public, private, protected etc) declared with any variable or method or class, then it means that there is a default
Advertisements
in project
the questions to be displayed like this test1 question1------[edit,delete] test1
in projects
the questions to be displayed like this, test1 question1------[edit,delete
recursions - Java Beginners
recursions  Can somebody help me with these four questions. Please. i am learning recursions in Java. Question1. Trying to find the value of result when its (5) from this code. public int result(int n) { if (n==1
Default constructor generate two random equations
{ public static void main(String[] args) { Question question1 = new
reduce complexity
(request.getParameter("testid")); int question1 = Integer.parseInt(request...); question.setQuestionId1(question1); question.setQuestionId2(question2
enabling and disabling a hyperlinks using javascript only
enabling and disabling a hyperlinks using javascript only  Hi! Anyone can help in enabling and disabling a hyperlinks?. Question1: There is a frame set containing 2 frames a source frame containing 1.jsp and a destination frame
enabling and disabling a hyperlinks using javascript only
enabling and disabling a hyperlinks using javascript only  Hi! can anyone help in enabling and disabling hyperlinks?. Question1: There is a frame set containing 2 frames a source frame containing 1.jsp and a destination frame
chn.ilp.test
(request.getParameter("testid")); int question1 = Integer.parseInt(request...(question1); question.setQuestionId2(question2); question.setQuestionId3

Ads