Given an array arr[] consisting of N integers, the task is to partition the array into two non-empty subarrays such that every element present in the right subarray is strictly greater than every element present in the left subarray. If it is possible to do so, then print the two resultant subarrays. Otherwise, print “Impossible”.
Examples:
Input: arr[] = {5, 3, 2, 7, 9}
Output:
5 3 2
7 9
Explanation:
One of the possible partition is {5, 3, 2} and {7, 9}.
The minimum of 2nd subarray {7} is greater than the maximum of the first subarray (5).
Input: arr[] = {1,1,1,1,1}
Output: Impossible
Explanation:
There is no partition possible for this array.
Naive Approach: The simplest approach is to traverse the array and for every index, check if the maximum of the first subarray is less than the minimum of the second subarray or not. If found to be true, then print the two subarrays.
C++
#include <bits/stdc++.h>
using namespace std;
void partitionArray(vector< int >& arr, int n)
{
for ( int i = 0; i < n - 1; i++) {
int leftMax = arr[0];
int j = 0;
for (; j <= i; j++) {
leftMax = max(leftMax, arr[j]);
}
int rightMin = arr[j];
for ( int k = j; k < n; k++) {
rightMin = min(rightMin, arr[k]);
}
if (rightMin > leftMax) {
for ( int l = 0; l <= i; l++) {
cout << arr[l] << " " ;
}
cout << endl;
for ( int o = j; o < n; o++) {
cout << arr[o] << " " ;
}
return ;
}
}
cout << "Impossible" << endl;
}
int main()
{
vector< int > arr = { 5, 3, 2, 7, 9 };
int N = 5;
partitionArray(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.List;
public class Gfg {
static void partitionArray(List<Integer> arr, int n)
{
for ( int i = 0 ; i < n - 1 ; i++) {
int leftMax = arr.get( 0 );
int j = 0 ;
for (; j <= i; j++) {
leftMax = Math.max(leftMax, arr.get(j));
}
int rightMin = arr.get(j);
for ( int k = j; k < n; k++) {
rightMin = Math.min(rightMin, arr.get(k));
}
if (rightMin > leftMax) {
for ( int l = 0 ; l <= i; l++) {
System.out.print(arr.get(l) + " " );
}
System.out.println();
for ( int o = j; o < n; o++) {
System.out.print(arr.get(o) + " " );
}
return ;
}
}
System.out.println( "Impossible" );
}
public static void main(String[] args)
{
List<Integer> arr = Arrays.asList( 5 , 3 , 2 , 7 , 9 );
int N = 5 ;
partitionArray(arr, N);
}
}
|
Python3
def partitionArray(arr, n):
for i in range ( 0 ,n - 1 ):
leftMax = arr[ 0 ];
j = 0 ;
for j in range ( 0 ,i + 1 ):
leftMax = max (leftMax, arr[j]);
j = i + 1 ;
rightMin = arr[j];
for k in range (j,n):
rightMin = min (rightMin, arr[k]);
if (rightMin > leftMax) :
for l in range ( 0 ,i + 1 ):
print (arr[l] , end = " " );
print ();
for o in range (j,n):
print (arr[o] , end = " " );
return ;
print ( "Impossible" );
arr = [ 5 , 3 , 2 , 7 , 9 ];
N = 5 ;
partitionArray(arr, N);
|
C#
using System;
using System.Collections.Generic;
class Gfg {
static void partitionArray(List< int > arr, int n)
{
for ( int i = 0; i < n - 1; i++) {
int leftMax = arr[0];
int j = 0;
for (; j <= i; j++) {
leftMax = Math.Max(leftMax, arr[j]);
}
int rightMin = arr[j];
for ( int k = j; k < n; k++) {
rightMin = Math.Min(rightMin, arr[k]);
}
if (rightMin > leftMax) {
for ( int l = 0; l <= i; l++) {
Console.Write(arr[l] + " " );
}
Console.WriteLine();
for ( int o = j; o < n; o++) {
Console.Write(arr[o] + " " );
}
return ;
}
}
Console.WriteLine( "Impossible" );
}
public static void Main( string [] args)
{
List< int > arr = new List< int >() { 5, 3, 2, 7, 9 };
int N = 5;
partitionArray(arr, N);
}
}
|
Javascript
function partitionArray(arr, n)
{
for (let i = 0; i < n - 1; i++) {
let leftMax = arr[0];
let j = 0;
for (; j <= i; j++) {
leftMax = Math.max(leftMax, arr[j]);
}
let rightMin = arr[j];
for (let k = j; k < n; k++) {
rightMin = Math.min(rightMin, arr[k]);
}
if (rightMin > leftMax) {
for (let l = 0; l <= i; l++) {
console.log(arr[l] , " " );
}
console.log( "\n" );
for (let o = j; o < n; o++) {
console.log(arr[o] , " " );
}
return ;
}
}
console.log( "Impossible" );
}
let arr = [ 5, 3, 2, 7, 9 ];
let N = 5;
partitionArray(arr, N);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by calculating the prefix maximum array and suffix minimum array which results in the constant time calculation of the maximum of the first subarray and minimum of 2nd subarray. Follow the steps below to solve the problem:
- Initialize an array, say min[], to store the minimum suffix array.
- Initialize 3 variables, say ind, mini and maxi, to store the index of partition, minimum of the suffix, and the maximum of the prefix respectively.
- Traverse the array in reverse and update mini as mini = min (mini, arr[i]). Assign mini to min[i].
- Now, traverse the array arr[] and perform the following operations:
- Update maxi as maxi =max(maxi, arr[i]).
- If i+1 < N as well as maxi < min[i+1], then print the partition made at index i and break.
- After completing the above steps, if none of the above cases are satisfied, then print “Impossible”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void partitionArray( int *a, int n)
{
int *Min = new int [n];
int Mini = INT_MAX;
for ( int i = n - 1; i >= 0; i--) {
Mini = min(Mini, a[i]);
Min[i] = Mini;
}
int Maxi = INT_MIN;
int ind = -1;
for ( int i = 0; i < n - 1; i++) {
Maxi = max(Maxi, a[i]);
if (Maxi < Min[i + 1]) {
ind = i;
break ;
}
}
if (ind != -1) {
for ( int i = 0; i <= ind; i++)
cout << a[i] << " " ;
cout << endl;
for ( int i = ind + 1; i < n; i++)
cout << a[i] << " " ;
}
else
cout << "Impossible" ;
}
int main()
{
int arr[] = { 5, 3, 2, 7, 9 };
int N = 5;
partitionArray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void partitionArray( int a[], int n)
{
int min[] = new int [n];
int mini = Integer.MAX_VALUE;
for ( int i = n - 1 ; i >= 0 ; i--) {
mini = Math.min(mini, a[i]);
min[i] = mini;
}
int maxi = Integer.MIN_VALUE;
int ind = - 1 ;
for ( int i = 0 ; i < n - 1 ; i++) {
maxi = Math.max(maxi, a[i]);
if (maxi < min[i + 1 ]) {
ind = i;
break ;
}
}
if (ind != - 1 ) {
for ( int i = 0 ; i <= ind; i++)
System.out.print(a[i] + " " );
System.out.println();
for ( int i = ind + 1 ; i < n; i++)
System.out.print(a[i] + " " );
}
else
System.out.println( "Impossible" );
}
public static void main(String[] args)
{
int arr[] = { 5 , 3 , 2 , 7 , 9 };
int N = arr.length;
partitionArray(arr, N);
}
}
|
Python3
import sys
def partitionArray(a, n):
Min = [ 0 ] * n
Mini = sys.maxsize
for i in range (n - 1 , - 1 , - 1 ):
Mini = min (Mini, a[i])
Min [i] = Mini
Maxi = - sys.maxsize - 1
ind = - 1
for i in range (n - 1 ):
Maxi = max (Maxi, a[i])
if (Maxi < Min [i + 1 ]):
ind = i
break
if (ind ! = - 1 ):
for i in range (ind + 1 ):
print (a[i], end = " " )
print ()
for i in range (ind + 1 , n, 1 ):
print (a[i], end = " " )
else :
print ( "Impossible" )
arr = [ 5 , 3 , 2 , 7 , 9 ]
N = 5
partitionArray(arr, N)
|
C#
using System;
class GFG {
static void partitionArray( int [] a, int n)
{
int [] min = new int [n];
int mini = Int32.MaxValue;
for ( int i = n - 1; i >= 0; i--) {
mini = Math.Min(mini, a[i]);
min[i] = mini;
}
int maxi = Int32.MinValue;
int ind = -1;
for ( int i = 0; i < n - 1; i++) {
maxi = Math.Max(maxi, a[i]);
if (maxi < min[i + 1]) {
ind = i;
break ;
}
}
if (ind != -1) {
for ( int i = 0; i <= ind; i++)
Console.Write(a[i] + " " );
Console.WriteLine();
for ( int i = ind + 1; i < n; i++)
Console.Write(a[i] + " " );
}
else
Console.Write( "Impossible" );
}
public static void Main( string [] args)
{
int [] arr = { 5, 3, 2, 7, 9 };
int N = arr.Length;
partitionArray(arr, N);
}
}
|
Javascript
<script>
function partitionArray(a , n)
{
var min = Array(n).fill(0);
var mini = Number.MAX_VALUE;
for (i = n - 1; i >= 0; i--) {
mini = Math.min(mini, a[i]);
min[i] = mini;
}
var maxi = Number.MIN_VALUE;
var ind = -1;
for (i = 0; i < n - 1; i++) {
maxi = Math.max(maxi, a[i]);
if (maxi < min[i + 1]) {
ind = i;
break ;
}
}
if (ind != -1) {
for (i = 0; i <= ind; i++)
document.write(a[i] + " " );
document.write( "<br/>" );
for (i = ind + 1; i < n; i++)
document.write(a[i] + " " );
}
else
document.write( "Impossible" );
}
var arr = [ 5, 3, 2, 7, 9 ];
var N = arr.length;
partitionArray(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)