Find the second largest number -coderinme


Second largest number

Find the second largest number in an array.
e.g 2 3 9 2 4 10 9 4 1 7 6
answer is 9

I method

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
  int num;
  // read the number of element in array
    cin>>num;
    // size of array
    int n=num;
    // making a vector or array
   vector <int> a;
  while(num--){
      int i;
      // read the element
      cin>>i;
    a.push_back(i);
  }
    // sort in ascending order
  sort(a.begin(),a.end());
    // print the 2nd largest
   cout<<a[n-2];
  return 0;
}

Second largest number
For more questionH.O.T.S

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.