
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if Queue Elements are pairwise consecutive in Python
Suppose we have a queue full of numbers. We have to check whether the consecutive elements in the queue are pairwise consecutive or not.
So, if the input is like que = [3,4,6,7,8,9], then the output will be True.
To solve this, we will follow these steps −
- q := define a queue and insert all elements from given list into q
- temp := a new list
- while q is not empty, do
- insert front element of queue into temp and delete front element from queue
- temp2 := a new list
- while temp is not empty, do
- insert last element of temp into temp2
- delete last element from temp
- result := True
- while size of temp2 > 1, do
- x := last element of temp2
- delete last element from temp2
- y := last element of temp2
- delete last element from temp2
- if |x - y| is not 1, then
- result := False
- insert x and y into q
- if size of temp2 is 1, then
- insert last element of temp2 into q
- return result
Example
Let us see the following implementation to get better understanding −
import queue def solve(que): q = queue.Queue() for i in que: q.put(i) temp = [] while q.qsize() != 0: temp.append(q.queue[0]) q.get() temp2 = [] while len(temp) != 0: temp2.append(temp[len(temp) - 1]) temp.pop() result = bool(True) while len(temp2) > 1: x = temp2[len(temp2) - 1] temp2.pop() y = temp2[len(temp2) - 1] temp2.pop() if abs(x - y) != 1: result = False q.put(x) q.put(y) if len(temp2) == 1: q.put(temp2[len(temp2) - 1]) return result que = [3,4,6,7,8,9] print(solve(que))
Input
[3,4,6,7,8,9]
Output
True
- Related Articles
- Check if the elements of stack are pairwise sorted in Python
- Check if array elements are consecutive in Python
- Product of all pairwise consecutive elements in an Arrays in C++
- Absolute Difference of all pairwise consecutive elements in an array (C++)?
- Check if list contains consecutive numbers in Python
- Check if moves in a stack or queue are possible or nots in Python
- Check if all array elements are distinct in Python
- Python – Check if elements index are equal for list elements
- Check if three consecutive elements in an array is identical in JavaScript
- Check if a queue can be sorted into another queue using a stack in Python
- Python - Check if all elements in a List are same
- Python - Check if all elements in a list are identical
- Python – Check if elements in a specific index are equal for list elements
- Check if a Linked List is Pairwise Sorted in C++
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python

Advertisements