Given an array and a range [lowVal, highVal], partition the array around the range such that array is divided in three parts.
1) All elements smaller than lowVal come first.
2) All elements in range lowVal to highVal come next.
3) All elements greater than highVal appear in the end.
The individual elements of three sets can appear in any order.
Examples:
Input: arr = [1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32]
lowVal = 14, highVal = 20
Output: arr = [1, 5, 4, 2, 3, 1, 14, 20, 20, 54, 87, 98, 32]
Input: arr = [1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32]
lowVal = 20, highVal = 20
Output: arr = [1, 14, 5, 4, 2, 3, 1, 20, 20, 54, 87, 98, 32]
We have existing solution for this problem please refer Three way partitioning of an array around a given range link. We can solve this problem quickly in python using List Comprehension. Approach is simple,
- Separate list in three parts, first one will contain elements less than lowVal, second will contain elements between lowVal and highVal, third will contain elements greater than highVal.
- Concatenate all three parts together.
# Function for Three way partitioning of an # array around a given range def threeWay(input, lowVal, highVal): # separate input list in three parts first = [ num for num in input if num<lowVal ] second = [ num for num in input if (num>=lowVal and num<=highVal) ] third = [ num for num in input if num>highVal ] # concatenate all three parts print(first + second + third) # Driver program if __name__ == "__main__": input = [1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32] lowVal = 14 highVal = 20 threeWay(input, lowVal, highVal) |
Output:
[1, 5, 4, 2, 3, 1, 14, 20, 20, 54, 87, 98, 32]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Display the Pandas DataFrame in table style and border around the table and not around the rows
- Python List Comprehension | Segregate 0's and 1's in an array list
- Python List Comprehension to find pair with given sum from two arrays
- Move all zeroes to end of array using List Comprehension in Python
- How to add a border around a NumPy array?
- numpy.around() in Python
- Python program to concatenate Strings around K
- Python - Get Confirmed, Recovered, Deaths cases of Corona around the globe
- Python Program to Converts Characters To Uppercase Around Numbers
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Difference between List comprehension and Lambda in Python
- Python | List comprehension vs * operator
- Python - Map vs List comprehension
- How to handle a Python Exception in a List Comprehension?
- Python - List Comprehension
- Python List Comprehension and Slicing
- Count set bits using Python List comprehension
- Python program to check if the list contains three consecutive common numbers in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

