Partition an array such into maximum increasing segments
Last Updated :
08 Sep, 2022
We are given an array of N integers, we need to partition the array into segments such that every element of a segment is greater than every element of previous segment. In other words, if we sort these individual segments, the whole array becomes sorted. We need to find a valid partition with maximum number of subarrays.
Examples:
input: arr[] = {3 1 2 4 100 7 9}
Output : 3
You should partition the array into the following subarrays: (3, 1, 2), (4) and (100, 7, 9).
Input : arr[] = {2 1 2 3 3 4 3}
Output : 5
Approach A greedy approach works really well as it leads to the following algorithm. Find the shortest prefix such that all the elements in the prefix are less than or equal to the elements in the rest of the array.
Consider this prefix as the first subarray of the partition. Recursively call the same algorithm on the rest of the array. Implementation wise, we can preprocess an array of max values for each prefix and another array of min values for each suffix. This way we can easily perform a check to see if a given prefix is a viable candidate for a partition subarray.
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
int sorted_partitions( int arr[], int n)
{
int right_min[n + 1];
right_min[n] = INT8_MAX;
for ( int i = n - 1; i >= 0; i--) {
right_min[i] = min(right_min[i + 1], arr[i]);
}
int partitions = 0;
for ( int current_max = arr[0], i = 0; i < n; i++) {
current_max = max(current_max, arr[i]);
if (current_max <= right_min[i + 1])
partitions++;
}
return partitions;
}
int main()
{
int arr[] = { 3, 1, 2, 4, 100, 7, 9 };
int n = sizeof (arr)/ sizeof (arr[0]);
int ans = sorted_partitions(arr,n);
cout << ans << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int sorted_partitions( int arr[])
{
int n = arr.length;
int [] right_min = new int [n + 1 ];
right_min[n] = Integer.MAX_VALUE;
for ( int i = n - 1 ; i >= 0 ; i--) {
right_min[i] = Math.min(right_min[i + 1 ], arr[i]);
}
int partitions = 0 ;
for ( int current_max = arr[ 0 ], i = 0 ; i < n; i++) {
current_max = Math.max(current_max, arr[i]);
if (current_max <= right_min[i + 1 ])
partitions++;
}
return partitions;
}
public static void main(String[] args)
{
int [] arr = new int [] { 3 , 1 , 2 , 4 , 100 , 7 , 9 };
int ans = sorted_partitions(arr);
System.out.println(ans);
}
}
|
Python 3
import sys
def sorted_partitions( arr, n):
right_min = [ 0 ] * (n + 1 )
right_min[n] = sys.maxsize
for i in range (n - 1 , - 1 , - 1 ):
right_min[i] = min (right_min[i + 1 ], arr[i])
partitions = 0
current_max = arr[ 0 ]
for i in range (n):
current_max = max (current_max, arr[i])
if (current_max < = right_min[i + 1 ]):
partitions + = 1
return partitions
arr = [ 3 , 1 , 2 , 4 , 100 , 7 , 9 ]
n = len (arr)
ans = sorted_partitions(arr, n)
print (ans)
|
C#
using System;
class GFG {
static int sorted_partitions( int []arr)
{
int n = arr.Length;
int [] right_min = new int [n + 1];
right_min[n] = int .MaxValue;
for ( int i = n - 1; i >= 0; i--) {
right_min[i] = Math.Min(right_min[i + 1], arr[i]);
}
int partitions = 0;
for ( int current_max = arr[0], i = 0; i < n; i++) {
current_max = Math.Max(current_max, arr[i]);
if (current_max <= right_min[i + 1])
partitions++;
}
return partitions;
}
public static void Main()
{
int [] arr = { 3, 1, 2, 4, 100, 7, 9 };
int ans = sorted_partitions(arr);
Console.WriteLine(ans);
}
}
|
PHP
<?php
function sorted_partitions( $arr , $n )
{
$right_min [ $n + 1] = array ();
$right_min [ $n ] = PHP_INT_MAX;
for ( $i = $n - 1; $i >= 0; $i --)
{
$right_min [ $i ] = min( $right_min [ $i + 1],
$arr [ $i ]);
}
$partitions = 0;
for ( $current_max = $arr [0],
$i = 0; $i < $n ; $i ++)
{
$current_max = max( $current_max , $arr [ $i ]);
if ( $current_max <= $right_min [ $i + 1])
$partitions ++;
}
return $partitions ;
}
$arr = array ( 3, 1, 2, 4, 100, 7, 9 );
$n = sizeof( $arr );
$ans = sorted_partitions( $arr , $n );
echo $ans , "\n" ;
?>
|
Javascript
<script>
function sorted_partitions(arr)
{
let n = arr.length;
let right_min = new Array(n + 1);
right_min.fill(0);
right_min[n] = Number.MAX_VALUE;
for (let i = n - 1; i >= 0; i--) {
right_min[i] = Math.min(right_min[i + 1], arr[i]);
}
let partitions = 0;
for (let current_max = arr[0], i = 0; i < n; i++) {
current_max = Math.max(current_max, arr[i]);
if (current_max <= right_min[i + 1])
partitions++;
}
return partitions;
}
let arr = [ 3, 1, 2, 4, 100, 7, 9 ];
let ans = sorted_partitions(arr);
document.write(ans);
</script>
|
Performance Analysis:
- Time Complexity: O(n), as single traversal is used in sorted_partitions() function.
- Space Complexity: O(n) ,we declared an array of size n+1 in sorted_partitions() function.
Share your thoughts in the comments
Please Login to comment...