Welcome to the daily solving of our PROBLEM OF THE DAY with Siddharth Hazra . We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Arrays but also build up problem-solving skills.
In this problem, we are given, an array of size n and a range [a, b]. The task is to partition the array around the range such that the array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified array.
Note: The generated output is 1 if you modify the given array successfully.
Geeky Challenge: Solve this problem in O(n) time complexity.
Example :
Input:
n = 5
array[] = {1, 2, 3, 3, 4}
[a, b] = [1, 2]
Output:
1
Explanation:
One possible arrangement is: {1, 2, 3, 3, 4}. If you return a valid arrangement, output will be 1.
Give the problem a try before going through the video. All the best!!!
Problem Link: https://www.geeksforgeeks.org/problems/three-way-partitioning/1