Minimum number of deques required to make the array sorted
Given an array arr containing N unique integers. The task is to calculate the minimum number of deques required to make the array sorted.
Example:
Input: arr[] = {3, 6, 0, 9, 5, 4}
Output: 2
Explanation:
Create a new deque d1 = {3}.
Create a new deque d2 = {6}.
Push 0 onto the front of d1; d1 = {0, 3}
Push 9 onto the back of d2; d2 = {6, 9}
Push 5 onto the front of d2; d2 = {5, 6, 9}
Push 4 onto the front of d2; d2 = {4, 5, 6, 9}
Hence 2 minimum 2 deques are required.
Input: arr[] = {50, 45, 55, 60, 65, 40, 70, 35, 30, 75}
Output: 1
Approach: The above problem can be solved greedily. Follow the below steps to solve the problem:
- Create two arrays fronts and backs which will store the front and back elements of all deques.
- Iterate for all elements in the array. For each element arr[i], the current element can be pushed in a pre-existing deque if:
- There exists a fronts[j] which is greater than arr[i] because this means that this arr[i] can be pushed in the front of this deque. But if there exists another element in between arr[i] and fronts[j] in the array arr, then it cannot be pushed because pushing will disturb the order of elements in deques such that these deques cannot be arranged in the form of a sorted array, even being individually sorted.
- Similarly, check for the array backs using the above-mentioned step.
- If an element cannot be pushed to an of the deque then another deque should be created for that element. So push the element in fronts as well as in backs array because it is both the front and back of the newly created deque.
- Now, return the size of array fronts (or backs) as the answer to this question.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDeques(vector< int > arr)
{
vector< int > fronts, backs;
for ( int i = 0; i < arr.size(); i++) {
bool hasBeenPushed = false ;
for ( int j = 0; j < fronts.size(); j++) {
if (arr[i] < fronts[j]) {
bool isSafe = true ;
for ( int k = 0; k < arr.size(); k++) {
if (arr[i] < arr[k]
&& arr[k] < fronts[j]) {
isSafe = false ;
break ;
}
}
if (isSafe) {
fronts[j] = arr[i];
hasBeenPushed = true ;
break ;
}
}
if (arr[i] > backs[j]) {
bool isSafe = true ;
for ( int k = 0; k < arr.size(); k++) {
if (arr[i] > arr[k]
&& arr[k] > backs[j]) {
isSafe = false ;
break ;
}
}
if (isSafe) {
backs[j] = arr[i];
hasBeenPushed = true ;
break ;
}
}
}
if (!hasBeenPushed) {
fronts.push_back(arr[i]);
backs.push_back(arr[i]);
}
}
return fronts.size();
}
int main()
{
vector< int > arr = { 3, 6, 0, 9, 5, 4 };
cout << minDeques(arr);
}
|
Java
import java.util.*;
class GFG{
public static int minDeques( int [] arr)
{
ArrayList<Integer> fronts = new ArrayList<Integer>();
ArrayList<Integer> backs = new ArrayList<Integer>();
for ( int i = 0 ; i < arr.length; i++) {
boolean hasBeenPushed = false ;
for ( int j = 0 ; j < fronts.size(); j++) {
if (arr[i] < fronts.get(j)) {
boolean isSafe = true ;
for ( int k = 0 ; k < arr.length; k++) {
if (arr[i] < arr[k]
&& arr[k] < fronts.get(j)) {
isSafe = false ;
break ;
}
}
if (isSafe) {
fronts.set(j, arr[i]);
hasBeenPushed = true ;
break ;
}
}
if (arr[i] > backs.get(j)) {
Boolean isSafe = true ;
for ( int k = 0 ; k < arr.length; k++) {
if (arr[i] > arr[k]
&& arr[k] > backs.get(j)) {
isSafe = false ;
break ;
}
}
if (isSafe) {
backs.set(j, arr[i]);
hasBeenPushed = true ;
break ;
}
}
}
if (!hasBeenPushed) {
fronts.add(arr[i]);
backs.add(arr[i]);
}
}
return fronts.size();
}
public static void main(String args[])
{
int [] arr = { 3 , 6 , 0 , 9 , 5 , 4 };
System.out.println(minDeques(arr));
}
}
|
Python3
def minDeques(arr):
fronts = []
backs = []
for i in range ( 0 , len (arr)):
hasBeenPushed = False
for j in range ( 0 , len (fronts)):
if (arr[i] < fronts[j]):
isSafe = True
for k in range ( 0 , len (arr)):
if (arr[i] < arr[k] and arr[k] < fronts[j]):
isSafe = False
break
if (isSafe):
fronts[j] = arr[i]
hasBeenPushed = True
break
if (arr[i] > backs[j]):
isSafe = True
for k in range ( 0 , len (arr)):
if (arr[i] > arr[k] and arr[k] > backs[j]):
isSafe = False
break
if (isSafe):
backs[j] = arr[i]
hasBeenPushed = True
break
if ( not hasBeenPushed):
fronts.append(arr[i])
backs.append(arr[i])
return len (fronts)
if __name__ = = "__main__" :
arr = [ 3 , 6 , 0 , 9 , 5 , 4 ]
print (minDeques(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int minDeques(List< int > arr)
{
List< int > fronts = new List< int >();
List< int > backs = new List< int >();
for ( int i = 0; i < arr.Count; i++) {
bool hasBeenPushed = false ;
for ( int j = 0; j < fronts.Count; j++) {
if (arr[i] < fronts[j]) {
bool isSafe = true ;
for ( int k = 0; k < arr.Count; k++) {
if (arr[i] < arr[k]
&& arr[k] < fronts[j]) {
isSafe = false ;
break ;
}
}
if (isSafe) {
fronts[j] = arr[i];
hasBeenPushed = true ;
break ;
}
}
if (arr[i] > backs[j]) {
bool isSafe = true ;
for ( int k = 0; k < arr.Count; k++) {
if (arr[i] > arr[k]
&& arr[k] > backs[j]) {
isSafe = false ;
break ;
}
}
if (isSafe) {
backs[j] = arr[i];
hasBeenPushed = true ;
break ;
}
}
}
if (!hasBeenPushed) {
fronts.Add(arr[i]);
backs.Add(arr[i]);
}
}
return fronts.Count;
}
public static void Main()
{
List< int > arr = new List< int >{ 3, 6, 0, 9, 5, 4 };
Console.WriteLine(minDeques(arr));
}
}
|
Javascript
<script>
function minDeques(arr)
{
let fronts = [],
backs = [];
for (let i = 0; i < arr.length; i++)
{
let hasBeenPushed = false ;
for (let j = 0; j < fronts.length; j++)
{
if (arr[i] < fronts[j]) {
let isSafe = true ;
for (let k = 0; k < arr.length; k++) {
if (arr[i] < arr[k] && arr[k] < fronts[j]) {
isSafe = false ;
break ;
}
}
if (isSafe) {
fronts[j] = arr[i];
hasBeenPushed = true ;
break ;
}
}
if (arr[i] > backs[j]) {
let isSafe = true ;
for (let k = 0; k < arr.length; k++) {
if (arr[i] > arr[k] && arr[k] > backs[j]) {
isSafe = false ;
break ;
}
}
if (isSafe) {
backs[j] = arr[i];
hasBeenPushed = true ;
break ;
}
}
}
if (!hasBeenPushed) {
fronts.push(arr[i]);
backs.push(arr[i]);
}
}
return fronts.length;
}
let arr = [3, 6, 0, 9, 5, 4];
document.write(minDeques(arr));
</script>
|
Time Complexity: O(N^3)
Auxiliary Space: O(N)
Last Updated :
26 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...