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++ program to find the largest // element present in the queue #include <bits/stdc++.h> using namespace std;
// Function which return // largest element of the queue int maxElement(queue< int > q)
{ // If queue is empty return -1
if (q.empty())
return -1;
// To store the largest element
int maxx = INT_MIN;
// Loop which iterate until
// queue becomes empty
while (!q.empty()) {
// Store the current front element
int temp = q.front();
// pop current front element
q.pop();
// Store maximum value between
// temp and maxx
maxx = max(maxx, temp);
}
// Return largest value
return maxx;
} // Driver Code int main()
{ queue< int > q;
// Pushing elements into queue
q.push(15);
q.push(27);
q.push(18);
// Call function and store
// return value into maxx
int maxx = maxElement(q);
// print the largest element
cout << maxx << endl;
return 0;
} // This code is contributed by Susobhan Akhuli |
// Java program to find the largest // element present in the queue import java.util.LinkedList;
import java.util.Queue;
public class Main {
// Function which returns the
//largest element of the queue
static int maxElement(Queue<Integer> q)
{
// If the queue is empty, return -1
if (q.isEmpty())
return - 1 ;
// To store the largest element
int maxx = Integer.MIN_VALUE;
// Loop which iterates until
//the queue becomes empty
while (!q.isEmpty()) {
// Store the current front element
int temp = q.peek();
// Remove the current front element
q.poll();
// Store the maximum value
//between temp and maxx
maxx = Math.max(maxx, temp);
}
// Return the largest value
return maxx;
}
// Driver Code
public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();
// Pushing elements into the queue
q.add( 15 );
q.add( 27 );
q.add( 18 );
// Call the function and store the
// return value into maxx
int maxx = maxElement(q);
// Print the largest element
System.out.println(maxx);
}
} // This code is contributed by Susobhan Akhuli |
# Python program to find the # largest element present # in the queue from queue import Queue
# Function which return largest element # of the queue def maxElement(q: Queue) - > int :
# If queue is empty return -1
if q.empty():
return - 1
# To store the largest element
maxx = float ( '-inf' )
# Loop which iterate until
# queue becomes empty
while not q.empty():
# Store the current front element
temp = q.get()
# pop current front element
# Store maximum value
# between temp and maxx
maxx = max (maxx, temp)
# Return largest value
return maxx
# Driver Code if __name__ = = '__main__' :
q = Queue()
# Pushing elements into queue
q.put( 15 )
q.put( 27 )
q.put( 18 )
# Call function and store
# return value into maxx
maxx = maxElement(q)
# print the largest element
print (maxx)
# This code is contributed by Susobhan Akhuli |
// C# program to find the largest // element present in the queue using System;
using System.Collections.Generic;
class GFG {
static int MaxElement(Queue< int > q)
{
// If queue is empty return -1
if (q.Count == 0)
return -1;
// To store the largest element
int max = int .MinValue;
// Loop which iterate until queue becomes empty
while (q.Count > 0) {
// Store the current front element
int temp = q.Dequeue();
// Store maximum value between temp and max
max = Math.Max(max, temp);
}
// Return largest value
return max;
}
static void Main()
{
Queue< int > q = new Queue< int >();
// Pushing elements into queue
q.Enqueue(15);
q.Enqueue(27);
q.Enqueue(18);
// Call function and store return value into max
int max = MaxElement(q);
// Print the largest element
Console.WriteLine(max);
// Keep the console window open
Console.ReadLine();
}
} // This code is contributed by Susobhan Akhuli |
// Javascript program to find the largest // element present in the queue // Function which returns the largest element of the queue function maxElement(q) {
// If queue is empty return -1
if (q.length === 0) {
return -1;
}
// To store the largest element
let maxx = Number.MIN_SAFE_INTEGER;
// Loop which iterates until queue becomes empty
while (q.length !== 0) {
// Store the current front element
let temp = q[0];
// Remove current front element
q.shift();
// Store maximum value between temp and maxx
maxx = Math.max(maxx, temp);
}
// Return largest value
return maxx;
} // Driver Code let q = []; // Pushing elements into queue q.push(15); q.push(27); q.push(18); // Call function and store return value into maxx let maxx = maxElement(q); // Print the largest element console.log(maxx); // This code is contributed by Susobhan Akhuli |
27
Time Complexity: O(N), where N is the number of elements in the queue.
Auxiliary Space: O(1)