You are not logged in. Please login at www.codechef.com to post your questions!

×

How do I find the second largest number when I input an array of numbers?

For example, if i enter 5 values, 5,10,3,5,7,8 I want a code to find the second largest value I dunno how to do it I'm new to this :) Don't h8 m8

asked 08 Sep '17, 21:31

crash21499's gravatar image

2★crash21499
74
accept rate: 0%


ask me if you have any questions

      #include <iostream>

      using namespace std;

  int main()
 {
int size;
cin >> size;
int array[size];

for(int i = 0;i<size;i++)
    cin >> array[i];


int largest = array[0];
int index = 0;
int secondLargest = array[0];
for(int i = 1;i<size;i++)
{
    if(largest<array[i])
    {
        index = i;
        largest = array[i];
    }

    if(i!=index)
          {
        if(secondLargest<array[i])
            secondLargest = array[i];
    }
}

cout << "second largest is " <<  secondLargest << endl;
return 0;

}

link

answered 09 Sep '17, 21:38

kunnu120's gravatar image

2★kunnu120
5189
accept rate: 5%

edited 10 Sep '17, 20:13

I want one where i input a random set of numbers myself and it shows the second highest num :(

(09 Sep '17, 23:17) crash214992★

bro just take the input Ok let me edit the solution for you

(10 Sep '17, 20:11) kunnu1202★

Okay I edited the solution first enter the size of the array and then your random set of numbers

(10 Sep '17, 20:15) kunnu1202★
1

THANKS A LOT!!

(12 Sep '17, 01:22) crash214992★

(sorry i'm yet to learn a lot about programming)

(12 Sep '17, 01:25) crash214992★

I'm glad it helped you :)

(12 Sep '17, 06:13) kunnu1202★
showing 5 of 6 show all

if you are beginner then first try to find the largest of all the elements..(it can be done if you know how to use loops.) after this you can remove this(largest) element(it's index) and then run the same code on the remaining .. :)

link

answered 08 Sep '17, 22:42

pk301's gravatar image

3★pk301
62710
accept rate: 16%

But what should I do after that? The syntax is confusing to me. Thanks for the reply though!!

(08 Sep '17, 23:43) crash214992★

You can sort the array first for ex

your array after getting sorted will look like this

3 5 5 7 8 10

so, you know that last element is the largest is 10

set the int max value equals to (max = array[size-1])

and then make a loop which will start from the end and go until it finds a different value than the max, that will be our second largest

for(int i = size-1; i>=0;i--)

{

if(array[i]!=max)

{

secondLargest = array[i];

break;

}

} this will give you the second largest number

and as pk301 mentioned that you can find the largest number but you don't have to delete the value just keep the track the index of the largest element and then loop through one more time and check

if(j(which was the index of largest)!=i) and array[i] is largest in the array after the maximum.

link

answered 08 Sep '17, 22:50

kunnu120's gravatar image

2★kunnu120
5189
accept rate: 5%

edited 08 Sep '17, 23:02

Is there a way to do it without sorting to keep it as efficient as possible? I'll try this to see if it works. Thanks! meow

(08 Sep '17, 23:52) crash214992★

It's showing a compiling error...

(09 Sep '17, 01:18) crash214992★

Show me your code

(09 Sep '17, 21:26) kunnu1202★

its working but the result isn't coming correctly...

include<iostream>

using namespace std;

int main() { int i = 0;

int run[6], second_max;
cout << "Enter 6 numbers";
for (int i = 0; i < 6; i++)
cin >> run[i];
second_max = 0;
for (int i = 0; i < 6; i++);
{
    int second_max = i;
}

cout << second_max << "send second max num of"<< run[second_max];

void waitforuserinput();
char exitchar;
cin >> exitchar;

}

(09 Sep '17, 23:22) crash214992★
link

answered 09 Sep '17, 04:16

beginner_1111's gravatar image

4★beginner_1111
240110
accept rate: 13%

It can be done with single traversal only :)

(09 Sep '17, 04:17) beginner_11114★

IT'S NOT WORKING AHHHHH

(09 Sep '17, 17:33) crash214992★

Dont h8 m8 .

(09 Sep '17, 21:55) vijju123 ♦♦4★

@crash21499 it is working

(09 Sep '17, 23:57) beginner_11114★
(10 Sep '17, 19:42) crash214992★

array = [] // some array

// first is the maximum of the arrray
first = max(array)

f_time=False ; second= 0

// iterating to find the next max

for i in range(len(array)):
  if second < array[i] and array[i] != first:
    second = array[i]
  elif array[i] == first :

   // in case there more than one elemnt that is the max
    if f _time :
      second = array[i]
    else :
      f _time= True

// second has the second max in the array

link

answered 09 Sep '17, 23:26

phantomhive's gravatar image

4★phantomhive
944
accept rate: 0%

it isn't working....

(10 Sep '17, 19:52) crash214992★

Wow,

This is exactly what I was looking for! Thanks for detailed and clear description!


dissertation editing

link

answered 10 Sep '17, 20:51

forrestpat's gravatar image

0★forrestpat
-239
accept rate: 0%

what.....?

(12 Sep '17, 01:23) crash214992★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,901
×23
×7
×4

question asked: 08 Sep '17, 21:31

question was seen: 1,933 times

last updated: 12 Sep '17, 06:13