
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
Let’s suppose we have given an array of unsorted integers of size N. The task is to find the distinct max and second max element which are present in the array. The array may contain duplicate elements also. So we have to find only distinct elements. For example,
Input-1 −
N = 5 A[ ] = { 2, 2, 1, 3, 4 }
Output −
4 3
Explanation − From the given array, we can see ‘4’ is the maximum and ‘3’ is the second maximum.
Input-2 −
N = 4 A[ ] = { 1,3,3,2 }
Output −
3 2
Explanation − from the given array of size 4, we can see ‘3’ is largest and ‘2’ is second largest so we will return 3 2 as output.
Approach to solve this problem
In the given array of size N, there may be some duplicate elements also. To find the maximum and second maximum element from the array we can initialize two variables that store the max and second max.
Initially, if the current element is greater than the max then we will store the value of it to the max and the value of max(previous) to the second max.
To find the distinct element, we will check whether the current element is equal to max or not. If the current value is not equal to the max and also greater than the second max, then we will replace the previous value of the second max with the current value.
Initialize and take input of N size of the array.
A function maxAndSecondMax(int arr[], int size) takes an array as input and the size of the array. Which returns the max and second max element of the given array.
Iterate over the array elements and find if the current element is greater than the max then store the current value to the max and previous value of max with the second max.
Otherwise, if the current value is greater than the second max, then replace the previous value with the current value. Also, the current value should not equal the max.
Check if the second max does not contain any value.
Return the max and second max as the final Output.
Example
#include<bits/stdc++.h> using namespace std; void maxAndSecondMax(int *arr, int size){ int max= INT_MIN; int s_max= INT_MIN; for(int i=0;i<size; ++i){ if(arr[i] >max){ s_max= max; max= arr[i]; } else if(arr[i]> s_max && arr[i]!= max){ s_max= arr[i]; } } if(s_max==INT_MIN){ s_max= -1; } cout<<max<<" "<<s_max; } int main(){ int N= 6; int A[N]= {1,3,2,5,6,3}; maxAndSecondMax(A,N); return 0; }
Output
If we will run the above code, then it will print the output as,
6 5
6 and 5 are the distinct elements in the array which are Maximum and Second maximum.
- Related Articles
- Write a program in C++ to find the missing positive number in a given array of unsorted integers
- Write a program in Java to find the missing positive number in a given array of unsorted integers
- Write a program in Python to find the maximum length of a string in a given Series
- Write a program in C++ to find the most frequent element in a given array of integers
- Find a pair with maximum product in array of Integers in C++
- Find maximum XOR of given integer in a stream of integers in C++
- Java Program to sort integers in unsorted array
- C++ program to find out the maximum possible tally from given integers
- Python program to find the second maximum value in Dictionary
- Program to find maximum value at a given index in a bounded array in Python
- Maximum sum of smallest and second smallest in an array in C++ Program
- Write a Python program to find the maximum value from first four rows in a given series
- Write a program in Python to resample a given time series data and find the maximum month-end frequency
- Write a program in Go language to find the element with the maximum value in an array
- Maximum number of Unique integers in Sub- Array of given sizes in C++
