Register
Sign In
Skip over navigation
Home
Add New Question
Jobs
Products
Find second max value in N size array
Question posted
on
08 2010
Rate question difficulty level
1
1 Votes
You need to find the second max number in an array , you can use only one iteration !
Add an answer
Share
|
7 Answers
Guest
Two ways: Sort array in descending order and fetch the 2nd element. Or use the following.
int SecondMax(int Array[], int ALength)
{
int Fmax,Smax;
Fmax=Smax=Araay[0];
for (int i = 0; i < ALength; i++)
{
if (Array[i] > Smax){
if (Array[i] < Fmax){
Smax = Array[i];
}
else{
Smax = Fmax;
Fmax = Array[i];
}
}
}
return Smax;
}
08/13/2010
Guest
Sorry to destroy your celebration, but both solutions seem to be wrong.
1. Sorting an array is at least O(n*log(n)), and not O(n), as instructed, so sorting is out of the question.
2. if Array[0] contains the biggest number, your solution will not work.
My solution:
int SecondMax(int array[], int length)
{
int max1, max2, i;
if(length <= 1) return -1; /* error */
max1=array[0];
max2=array[1];
if(max1
{
max1=array[1];
max2=array[0];
}
for (i = 2; i < length; i++)
{
if (array[i] > max2)
{
if (array[i] < max1)
{
max2 = array[i];
}
else
{
max2 = max1;
max1 = array[i];
}
}
}
return max2;
}
08/14/2010
Eeotime4550
I am writing a research paper and collecting information on this topic. Your post is one of the better that I have read. Thank you for putting this information into one location.
payday loan
03/07/2012
ztimehowa
I have been seeking information on this topic for the past few hours and found your post to be well written and has solid information.
Love Sms
03/07/2012
www.kinderspielzeugonlineversand.com
I have found your blog long ago and continue reading it up-to-date! But now I have decided to register and write a comment for those who are here for the first time! Do not pass by!! Do not look at the site design! The content is surely what you need from this topic! Stay with us and enjoy!
www.kinderspielzeugonlineversand.com
05/04/2012
Richard John
Alright i execute the code and there wereno errors and you are right on what you say "further implementations on the loop, there is no value originate which is superior than a[n-2]." I was going to write if(a[i] != a[n-2]) secmax = a[i]; as there may be same numbers. I am currently enrolled into my Master's program in computer science at
Headway University
and regarding this topic hopefully my input will help you for sure.
05/08/2012
lala
Coach Outlet
Coach Factory
Coach Outlet Online
Coach Outlet Online
Coach Factory Outlet
Coach Tote Bags
05/17/2012
Add an answer*
Your name
Email
Enter the code shown above:
please wait..
Location:
Israel
Tags:
logic
,
developer
,
algorithem
Occupations:
Computer\Network Security
add a question
Now hiring!
IT Sourcing Procurement Analyst Software Service Infrastructure
CVS Caremark
Woonsocket, RI
Strategy, including: Procurement Execution ?Maintain IT procurement process in collaboration with IT stakeholders ... legal risk mitigation strategy are addressed ?Support the development of executive briefings summarizing contract...
---------------------------
Software Development Analyst/ .NET Developer- Health IT
Northrop Grumman
Atlanta, GA
Sharp programming skills, eagerness for challenging development opportunities, and the ability to work with a ... of information security concepts and principles, as it pertains to application/service development and...
---------------------------
(IT Applications Analyst Developer Lead
Halliburton
Houston, TX
Houston, TX DUTIES: Responsible for the development, acquisition, maintenance and support of applications in assigned ... plus 7 years of progressive experience in Systems Development and Application Support, including analysis,...
---------------------------
Mgr Software Development 2-Health IT
Northrop Grumman
Little Rock, AR
and software developers. Works with members of the development team to ensure that all legacy data is ... years of relevant experience - Experience with IT Project Development Lifecycle - Experience converting data for...
---------------------------
IT Specialist (Applications Development)
Parsons
Houston, TX
in software complexity. Makes a recommendation to senior IT management and the end user. Uses charts, diagrams and ... guidance to lower-level IT staff assigned to an application development project. * Develops functional specifications to...
---------------------------
Software Development Engineer (IT Security)
Amazon.com
Seattle, WA
Role Description Amazon.com is looking for expert Software Development Engineers to develop tools supporting the IT ... for you. You will participate in every level of software development from requirements gathering to operational...
---------------------------
Software Development Engineer - Kindle with Special Offers, AmazonLocal
Amazon.com
Seattle, WA
Software Developement Engineer - Kindle Application Development, AmazonLocal AmazonLocal is looking for exceptional, ... or related field * 2+ years professional experience in software development * Fundamentals in object-oriented...
---------------------------
Manager Software Development 2-Health IT
Northrop Grumman
Little Rock, AR
and software developers. Works with members of the development team to ensure that all legacy data is ... years of relevant experience - Experience with IT Project Development Lifecycle - Experience converting data for...
Jobs
by
Simply
Hired
View all jobs