Share on Google+Share on Google+

John Kamau
question1
1 Answer(s)      6 years and 9 months ago
Posted in : Java Beginners
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

Ads

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
Default constructor generate two random equations
{ public static void main(String[] args) { Question question1 = new
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
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
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads