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.
Python3
# 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]
Approach#2: Using filter()+lambda()
In this approach, we first use the filter() function along with a lambda function to filter out the elements from the list that are less than lowVal. Similarly, we filter out the elements that are greater than highVal. Finally, we concatenate the filtered lists along with the list comprehension.
Algorithm
1. Create three lists using filter() and lambda function to filter out elements less than lowVal, elements between lowVal and highVal, and elements greater than highVal respectively.
2. Concatenate these three lists to form the final list.
Python3
arr = [ 1 , 14 , 5 , 20 , 4 , 2 , 54 , 20 , 87 , 98 , 3 , 1 , 32 ] lowVal = 14 highVal = 20 arr = list ( filter ( lambda x: x < lowVal, arr)) + list ( filter ( lambda x: lowVal < = x < = highVal, arr)) + list ( filter ( lambda x: x > highVal, arr)) print (arr) |
[1, 5, 4, 2, 3, 1, 14, 20, 20, 54, 87, 98, 32]
Time complexity: O(n), The filter() function has a time complexity of O(n). We use filter() three times, so the time complexity of filter() is 3*O(n), which is O(n). Concatenating the three lists has a time complexity of O(n). Therefore, the total time complexity of the code is O(n).
Space complexity: O(n) We use filter() to create three lists, each containing at most n elements. The size of the final list is n. Therefore, the space complexity of the code is O(n).