Maximum number of partitions that can be sorted individually to make sorted
Last Updated :
16 Feb, 2023
Given an array arr[] of size n such that elements of arr[] in range [0, 1, ..n-1] where every number is present at most once. Our task is to divide the array into maximum number of partitions that can be sorted individually, then concatenated to make the whole array sorted.
Examples :
Input : arr[] = [2, 1, 0, 3]
Output : 2
If divide arr[] into two partitions
{2, 1, 0} and {3}, sort them and concatenate
then, we get the whole array sorted.
Input : arr[] = [2, 1, 0, 3, 4, 5]
Output : 4
The maximum number of partitions are four, we
get these partitions as {2, 1, 0}, {3}, {4}
and {5}
Input : arr[] = [0, 1, 2, 3, 4, 5]
Output : 6
The maximum number of partitions are six, we
get these partitions as {0}, {1}, {2}, {3}, {4}
and {5}
Approach:
The given code finds the maximum number of partitions that can be made from an array of integers. The code defines a function "maxPartitions" that takes an array and its length as input. The function then uses a loop to iterate through each element of the array and find the maximum element in the prefix ending at the current index. If the maximum element is equal to the current index, the function increments the count of partitions as a new partition can be made ending at that index. Finally, the function returns the count of partitions. The main function initializes an array with values and calls the "maxPartitions" function with the array and its length as inputs. The result of the function is then printed to the console.
Algorithm to find maximum partitions:
- Initialize a variable "ans" to store the count of partitions and a variable "max_so_far" to store the maximum element in the prefix of the array.
- Loop through each element in the array.
a. Set "max_so_far" to the maximum of "max_so_far" and the current element.
b. If "max_so_far" is equal to the current index, increment the value of "ans" by 1. - Return the value of "ans" as the result.
The idea is based on the fact that if an element at i is maximum of prefix arr[0..i], then we can make a partition ending with i.
C++
// CPP program to find Maximum number of partitions such
// that we can get a sorted array.
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum partitions.
int maxPartitions(int arr[], int n)
{
int ans = 0, max_so_far = 0;
for (int i = 0; i < n; ++i) {
// Find maximum in prefix arr[0..i]
max_so_far = max(max_so_far, arr[i]);
// If maximum so far is equal to index, we can make
// a new partition ending at index i.
if (max_so_far == i)
ans++;
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 1, 0, 2, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxPartitions(arr, n);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
C
// C program to find Maximum number of partitions such that
// we can get a sorted array.
#include <stdio.h>
// Find maximum between two numbers.
int max(int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
// Function to find maximum partitions.
int maxPartitions(int arr[], int n)
{
int ans = 0, max_so_far = 0;
for (int i = 0; i < n; ++i) {
// Find maximum in prefix arr[0..i]
max_so_far = max(max_so_far, arr[i]);
// If maximum so far is equal to index, we can make
// a new partition ending at index i.
if (max_so_far == i)
ans++;
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 1, 0, 2, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", maxPartitions(arr, n));
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
Java
// java program to find Maximum number of partitions such
// that we can get a sorted array
import java.io.*;
class GFG {
// Function to find maximum partitions.
static int maxPartitions(int arr[], int n)
{
int ans = 0, max_so_far = 0;
for (int i = 0; i < n; ++i) {
// Find maximum in prefix arr[0..i]
max_so_far = Math.max(max_so_far, arr[i]);
// If maximum so far is equal to index, we can
// make a new partition ending at index i.
if (max_so_far == i)
ans++;
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 0, 2, 3, 4 };
int n = arr.length;
System.out.println(maxPartitions(arr, n));
}
}
// This code is contributed by Aditya Kumar (adityakumar129)
Python3
# Python3 program to find Maximum
# number of partitions such that
# we can get a sorted array.
# Function to find maximum partitions.
def maxPartitions(arr, n):
ans = 0; max_so_far = 0
for i in range(0, n):
# Find maximum in prefix arr[0..i]
max_so_far = max(max_so_far, arr[i])
# If maximum so far is equal to
# index, we can make a new partition
# ending at index i.
if (max_so_far == i):
ans += 1
return ans
# Driver code
arr = [1, 0, 2, 3, 4]
n = len(arr)
print(maxPartitions(arr, n))
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# program to find Maximum number of partitions
// such that we can get a sorted array
using System;
class GFG
{
// Function to find maximum partitions.
static int maxPartitions(int []arr, int n)
{
int ans = 0, max_so_far = 0;
for (int i = 0; i < n; ++i)
{
// Find maximum in prefix arr[0..i]
max_so_far = Math.Max(max_so_far, arr[i]);
// If maximum so far is equal to index,
// we can make a new partition ending at
// index i.
if (max_so_far == i)
ans++;
}
return ans;
}
// Driver code
public static void Main ()
{
int []arr = { 1, 0, 2, 3, 4 };
int n = arr.Length;
Console.Write (maxPartitions(arr, n));
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP program to find Maximum
// number of partitions such
// that we can get a sorted array.
// Function to find maximum partitions.
function maxPartitions($arr, $n)
{
$ans = 0;
$max_so_far = 0;
for ($i = 0; $i < $n; ++$i) {
// Find maximum in prefix arr[0..i]
$max_so_far = max($max_so_far, $arr[$i]);
// If maximum so far is equal to index,
// we can make a new partition ending at
// index i.
if ($max_so_far == $i)
$ans++;
}
return $ans;
}
// Driver code
{
$arr = array(1, 0, 2, 3, 4);
$n = sizeof($arr) / sizeof($arr[0]);
echo maxPartitions($arr, $n);
return 0;
}
// This code is contributed by nitin mittal
?>
JavaScript
<script>
// Javascript program to find Maximum number of partitions
// such that we can get a sorted array.
// Function to find maximum partitions.
function maxPartitions(arr, n)
{
let ans = 0, max_so_far = 0;
for (let i = 0; i < n; ++i) {
// Find maximum in prefix arr[0..i]
max_so_far = Math.max(max_so_far, arr[i]);
// If maximum so far is equal to index,
// we can make a new partition ending at
// index i.
if (max_so_far == i)
ans++;
}
return ans;
}
let arr = [ 1, 0, 2, 3, 4 ];
let n = arr.length;
document.write(maxPartitions(arr, n));
// This code is contributed by divyesh072019.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize partitions that if sorted individually makes the whole Array sorted Given an array arr[]. The task is to divide arr[] into the maximum number of partitions, such that, those partitions if sorted individually make the whole array sorted.Examples:Input: arr[] = { 28, 9, 18, 32, 60, 50, 75, 70 }Output: 4Explanation: Following are the partitions in which the array is di
5 min read
Maximize the number of local maxima's with minimum operations Given an array A[] of size N (where N is even), Local maxima are array elements strictly greater in value than its adjacent elements i.e., A[i - 1] < A[i] > A[i + 1]. For all 2 ⤠i ⤠N - 1. The first and last element of the array can never be Local maxima. The task is to maximize the number of
15+ min read
Count number of ways to partition a set into k subsets Given two numbers n and k where n represents a number of elements in a set, find a number of ways to partition the set into k subsets.Example: Input: n = 3, k = 2Output: 3Explanation: Let the set be [1, 2, 3], we can partition it into 3 subsets in the following ways [[1,2], [3]], [[1], [2,3]], [[1,3
15+ min read
Minimum partitions of maximum size 2 and sum limited by given value Given an array arr[] of positive numbers, find minimum number of sets in array which satisfy following property, A set can contain maximum two elements in it. The two elements need not to be contiguous. Sum of elements of set should be less than or equal to given Key. It may be assumed that given ke
6 min read
Count numbers present in partitions of N Given an integer N, the task is to count the numbers in ordered integer partitions of N.Examples: Input: N = 3 Output: 8 Integer partitions of N(=3) are {{1 + 1 + 1}, {1 + 2}, {2 + 1}, {3}}. Numbers in integer partition of N are:{1, 1, 1, 1, 2, 2, 1, 3} Therefore, the count of numbers in integer par
3 min read