Split an array into minimum number of non-increasing or non-decreasing subarrays
Given an array arr[] of size N, the task is to split the given array into a minimum number of subarrays such that elements of each subarray are either in non-increasing order or non-decreasing order.
Examples:
Input: arr[] = {2, 3, 9, 5, 4, 6, 8}
Output: 3
Explanation: Split the array into 3 subarrays following three subarrays:
- {2, 3, 9} (non-decreasing)
- {5, 4} (non-increasing)
- {6, 8} (non-decreasing)
Input: arr[] = {2, 5, 3, 3, 4, 5, 0, 2, 1, 0}
Output: 4
Explanation: Split the array into following 4 subarray:
- {2, 5} (non-decreasing)
- {3, 3, 4, 5} (non-decreasing)
- {0, 2} (non-decreasing)
- {1, 0} (non-increasing)
Approach: To minimize the number of subarrays, the size of each subarray should be maximized. It can be done by placing the elements in subarrays greedily.
Follow the steps below to solve the problem:
- Initialize a variable, say ans, with 1 to store the required result and current with N to keep track of the order of the current sequence, whether it is non-decreasing(I), non-increasing(D), or none(N).
- Now, iterate over the array in the range [1, N – 1]:
- If the current is equal to N, do the following:
- If arr[i] < arr[i-1] then update current as D.
- Otherwise, if arr[i] > arr[i-1], then update current as I.
- Otherwise, update current as N.
- If the current is equal to I, do the following:
- If arr[i]?arr[i-1] then update current as I.
- Otherwise, update current as N and increment ans by 1.
- Otherwise, do the following:
- If arr[i] ? arr[i-1], then update current as D.
- Otherwise, update current as N and increment ans by 1.
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumSubarrays( int arr[], int n)
{
char current = 'N' ;
int answer = 1;
for ( int i = 1; i < n; i++) {
if (current == 'N' ) {
if (arr[i] < arr[i - 1]) {
current = 'D' ;
}
else if (arr[i] == arr[i - 1]) {
current = 'N' ;
}
else {
current = 'I' ;
}
}
else if (current == 'I' ) {
if (arr[i] >= arr[i - 1]) {
current = 'I' ;
}
else {
current = 'N' ;
answer += 1;
}
}
else {
if (arr[i] <= arr[i - 1]) {
current = 'D' ;
}
else {
current = 'N' ;
answer += 1;
}
}
}
cout<<answer;
}
int main() {
int arr[] = { 2, 3, 9, 5, 4, 6, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
minimumSubarrays(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void minimumSubarrays( int [] arr, int n)
{
char current = 'N' ;
int answer = 1 ;
for ( int i = 1 ; i < n; i++) {
if (current == 'N' ) {
if (arr[i] < arr[i - 1 ]) {
current = 'D' ;
}
else if (arr[i] == arr[i - 1 ]) {
current = 'N' ;
}
else {
current = 'I' ;
}
}
else if (current == 'I' ) {
if (arr[i] >= arr[i - 1 ]) {
current = 'I' ;
}
else {
current = 'N' ;
answer += 1 ;
}
}
else {
if (arr[i] <= arr[i - 1 ]) {
current = 'D' ;
}
else {
current = 'N' ;
answer += 1 ;
}
}
}
System.out.print(answer);
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 9 , 5 , 4 , 6 , 8 };
int n = arr.length;
minimumSubarrays(arr, n);
}
}
|
Python3
def minimumSubarrays(arr, n):
current = 'N'
answer = 1
for i in range ( 1 , n):
if (current = = 'N' ):
if (arr[i] < arr[i - 1 ]):
current = 'D'
elif (arr[i] = = arr[i - 1 ]):
current = 'N'
else :
current = 'I'
elif (current = = 'I' ):
if (arr[i] > = arr[i - 1 ]):
current = 'I'
else :
current = 'N'
answer + = 1
else :
if (arr[i] < = arr[i - 1 ]):
current = 'D'
else :
current = 'N'
answer + = 1
print (answer)
if __name__ = = '__main__' :
arr = [ 2 , 3 , 9 , 5 , 4 , 6 , 8 ]
n = len (arr)
minimumSubarrays(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void minimumSubarrays( int [] arr, int n)
{
char current = 'N' ;
int answer = 1;
for ( int i = 1; i < n; i++) {
if (current == 'N' ) {
if (arr[i] < arr[i - 1]) {
current = 'D' ;
}
else if (arr[i] == arr[i - 1]) {
current = 'N' ;
}
else {
current = 'I' ;
}
}
else if (current == 'I' ) {
if (arr[i] >= arr[i - 1]) {
current = 'I' ;
}
else {
current = 'N' ;
answer += 1;
}
}
else {
if (arr[i] <= arr[i - 1]) {
current = 'D' ;
}
else {
current = 'N' ;
answer += 1;
}
}
}
Console.Write(answer);
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 9, 5, 4, 6, 8 };
int n = arr.Length;
minimumSubarrays(arr, n);
}
}
|
Javascript
<script>
function minimumSubarrays(arr,n)
{
let current = 'N' ;
let answer = 1;
for (let i = 1; i < n; i++) {
if (current == 'N' ) {
if (arr[i] < arr[i - 1]) {
current = 'D' ;
}
else if (arr[i] == arr[i - 1]) {
current = 'N' ;
}
else {
current = 'I' ;
}
}
else if (current == 'I' ) {
if (arr[i] >= arr[i - 1]) {
current = 'I' ;
}
else {
current = 'N' ;
answer += 1;
}
}
else {
if (arr[i] <= arr[i - 1]) {
current = 'D' ;
}
else {
current = 'N' ;
answer += 1;
}
}
}
document.write(answer);
}
let arr = [ 2, 3, 9, 5, 4, 6, 8 ];
let n = arr.length;
minimumSubarrays(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
26 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...