Reduce array to longest sorted array possible by removing either half of given array in each operation
Given an array arr[] of size N (always power of 2), the task is to find the length of the longest sorted array to which the given array can be reduced by removing either half of the array at each operation.
Examples:
Input: arr[] = { 11, 12, 1, 2, 13, 14, 3, 4 }
Output: 2
Explanation:
Removal of the first half of arr[] modifies arr[] to {13, 14, 3, 4}.
Removal of the first half of arr[] modifies arr[] to {3, 4}.
Therefore, the length of the longest sorted array possible is 2 which is the maximum possible.
Input: arr[] = { 1, 2, 2, 4 }
Output: 4
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say MaxLength, to store the maximum length of the sorted array that can be obtained by performing the given operations.
- Recursively partition the array into two equal halves and for each half, check if the partition of the array is sorted or not. If found to be true, then update MaxLength to the length of the partition.
- Finally, print the value of MaxLength.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isSortedparitions( int arr[],
int i, int j)
{
for ( int k = i + 1; k <= j; k++) {
if (arr[k] < arr[k - 1]) {
return 0;
}
}
return 1;
}
int partitionsArr( int arr[],
int i, int j)
{
if (i >= j)
return 1;
bool flag = isSortedparitions(
arr, i, j);
if (flag) {
return (j - i + 1);
}
int mid = (i + j) / 2;
int X = partitionsArr(arr, i, mid);
int Y = partitionsArr(arr, mid + 1, j);
return max(X, Y);
}
int main()
{
int arr[] = { 11, 12, 1, 2,
13, 14, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << partitionsArr(
arr, 0, N - 1);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int isSortedparitions( int arr[],
int i, int j)
{
for ( int k = i + 1 ; k <= j; k++)
{
if (arr[k] < arr[k - 1 ])
{
return 0 ;
}
}
return 1 ;
}
static int partitionsArr( int arr[], int i,
int j)
{
if (i >= j)
return 1 ;
int flag = ( int )isSortedparitions(arr, i, j);
if (flag != 0 )
{
return (j - i + 1 );
}
int mid = (i + j) / 2 ;
int X = partitionsArr(arr, i, mid);
int Y = partitionsArr(arr, mid + 1 , j);
return Math.max(X, Y);
}
public static void main(String[] args)
{
int arr[] = { 11 , 12 , 1 , 2 ,
13 , 14 , 3 , 4 };
int N = arr.length;
System.out.print(partitionsArr(
arr, 0 , N - 1 ));
}
}
|
Python3
def isSortedparitions(arr, i, j):
for k in range (i + 1 , j + 1 ):
if (arr[k] < arr[k - 1 ]):
return 0
return 1
def partitionsArr(arr, i, j):
if (i > = j):
return 1
flag = int (isSortedparitions(arr, i, j))
if (flag ! = 0 ):
return (j - i + 1 )
mid = (i + j) / / 2
X = partitionsArr(arr, i, mid);
Y = partitionsArr(arr, mid + 1 , j)
return max (X, Y)
if __name__ = = '__main__' :
arr = [ 11 , 12 , 1 , 2 , 13 , 14 , 3 , 4 ]
N = len (arr)
print (partitionsArr(arr, 0 , N - 1 ))
|
C#
using System;
class GFG{
static int isSortedparitions( int [] arr,
int i, int j)
{
for ( int k = i + 1; k <= j; k++)
{
if (arr[k] < arr[k - 1])
{
return 0;
}
}
return 1;
}
static int partitionsArr( int [] arr, int i,
int j)
{
if (i >= j)
return 1;
int flag = ( int )isSortedparitions(arr, i, j);
if (flag != 0)
{
return (j - i + 1);
}
int mid = (i + j) / 2;
int X = partitionsArr(arr, i, mid);
int Y = partitionsArr(arr, mid + 1, j);
return Math.Max(X, Y);
}
public static void Main()
{
int [] arr = { 11, 12, 1, 2,
13, 14, 3, 4 };
int N = arr.Length;
Console.Write(partitionsArr(
arr, 0, N - 1));
}
}
|
Javascript
<script>
function isSortedparitions(arr, i, j)
{
for ( var k = i + 1; k <= j; k++) {
if (arr[k] < arr[k - 1]) {
return 0;
}
}
return 1;
}
function partitionsArr(arr, i, j)
{
if (i >= j)
return 1;
var flag = isSortedparitions(
arr, i, j);
if (flag) {
return (j - i + 1);
}
var mid = parseInt((i + j) / 2);
var X = partitionsArr(arr, i, mid);
var Y = partitionsArr(arr, mid + 1, j);
return Math.max(X, Y);
}
var arr = [11, 12, 1, 2,
13, 14, 3, 4 ];
var N = arr.length;
document.write( partitionsArr(
arr, 0, N - 1));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Last Updated :
18 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...