Find Largest element in a Queue
Last Updated :
04 Aug, 2023
Given a queue of integers, the task is to write a program that efficiently finds the largest element in that queue. Return -1 if the queue is empty.
Examples:
Input: Queue = {15, 27, 18}
Output: 27
Explanation: Among 15(front), 27 and 18(back), 27 is the largest.
Input: Queue = {12, 25, 29, 16, 32}
Output: 32
Explanation: Among 12(front), 25, 29, 16 and 32(back), 32 is the largest.
Naive Approach: The basic way to solve the problem is as follows:
The naive approach to finding the largest element in a queue is to just pop each element from the queue and store them in an array/vector. Then find largest element in that array.
Time Complexity: O(N), where N is the number of elements in the queue.
Auxiliary Space: O(N)
Efficient Approach: The efficient approach is to find the maximum/largest element of the queue during popping elements from the queue as follows:
- Store the current front element in the temp variable and then pop that element.
- Now, compare that temp with maxx and store the maximum value into maxx.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxElement(queue< int > q)
{
if (q.empty())
return -1;
int maxx = INT_MIN;
while (!q.empty()) {
int temp = q.front();
q.pop();
maxx = max(maxx, temp);
}
return maxx;
}
int main()
{
queue< int > q;
q.push(15);
q.push(27);
q.push(18);
int maxx = maxElement(q);
cout << maxx << endl;
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int maxElement(Queue<Integer> q)
{
if (q.isEmpty())
return - 1 ;
int maxx = Integer.MIN_VALUE;
while (!q.isEmpty()) {
int temp = q.peek();
q.poll();
maxx = Math.max(maxx, temp);
}
return maxx;
}
public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();
q.add( 15 );
q.add( 27 );
q.add( 18 );
int maxx = maxElement(q);
System.out.println(maxx);
}
}
|
Python3
from queue import Queue
def maxElement(q: Queue) - > int :
if q.empty():
return - 1
maxx = float ( '-inf' )
while not q.empty():
temp = q.get()
maxx = max (maxx, temp)
return maxx
if __name__ = = '__main__' :
q = Queue()
q.put( 15 )
q.put( 27 )
q.put( 18 )
maxx = maxElement(q)
print (maxx)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int MaxElement(Queue< int > q)
{
if (q.Count == 0)
return -1;
int max = int .MinValue;
while (q.Count > 0) {
int temp = q.Dequeue();
max = Math.Max(max, temp);
}
return max;
}
static void Main()
{
Queue< int > q = new Queue< int >();
q.Enqueue(15);
q.Enqueue(27);
q.Enqueue(18);
int max = MaxElement(q);
Console.WriteLine(max);
Console.ReadLine();
}
}
|
Javascript
function maxElement(q) {
if (q.length === 0) {
return -1;
}
let maxx = Number.MIN_SAFE_INTEGER;
while (q.length !== 0) {
let temp = q[0];
q.shift();
maxx = Math.max(maxx, temp);
}
return maxx;
}
let q = [];
q.push(15);
q.push(27);
q.push(18);
let maxx = maxElement(q);
console.log(maxx);
|
Time Complexity: O(N), where N is the number of elements in the queue.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...