给定一个大小为N的数组arr[] (总是 2 的幂),任务是找到最长的排序数组的长度,通过在每次操作中删除数组的任何一半,可以将给定数组减少到该长度。
例子:
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
处理方法:按照以下步骤解决问题:
- 初始化一个变量,比如MaxLength ,以存储通过执行给定操作可以获得的排序数组的最大长度。
- 递归地将数组分成两个相等的一半,对于每一半,检查数组的分区是否已排序。如果发现为真,则将MaxLength更新为分区的长度。
- 最后,打印MaxLength的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check if the subarray
// arr[i..j] is a sorted subarray or not
int isSortedparitions(int arr[],
int i, int j)
{
// Traverse all elements of
// the subarray arr[i...j]
for (int k = i + 1; k <= j; k++) {
// If the previous element of the
// subarray exceeds current element
if (arr[k] < arr[k - 1]) {
// Return 0
return 0;
}
}
// Return 1
return 1;
}
// Recursively partition the array
// into two equal halves
int partitionsArr(int arr[],
int i, int j)
{
// If atmost one element is left
// in the subarray arr[i..j]
if (i >= j)
return 1;
// Checks if subarray arr[i..j] is
// a sorted subarray or not
bool flag = isSortedparitions(
arr, i, j);
// If the subarray arr[i...j]
// is a sorted subarray
if (flag) {
return (j - i + 1);
}
// Stores middle element
// of the subarray arr[i..j]
int mid = (i + j) / 2;
// Recursively partition the current
// subarray arr[i..j] into equal halves
int X = partitionsArr(arr, i, mid);
int Y = partitionsArr(arr, mid + 1, j);
return max(X, Y);
}
// Driver Code
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
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to check if the subarray
// arr[i..j] is a sorted subarray or not
static int isSortedparitions(int arr[],
int i, int j)
{
// Traverse all elements of
// the subarray arr[i...j]
for(int k = i + 1; k <= j; k++)
{
// If the previous element of the
// subarray exceeds current element
if (arr[k] < arr[k - 1])
{
// Return 0
return 0;
}
}
// Return 1
return 1;
}
// Recursively partition the array
// into two equal halves
static int partitionsArr(int arr[], int i,
int j)
{
// If atmost one element is left
// in the subarray arr[i..j]
if (i >= j)
return 1;
// Checks if subarray arr[i..j] is
// a sorted subarray or not
int flag = (int)isSortedparitions(arr, i, j);
// If the subarray arr[i...j]
// is a sorted subarray
if (flag != 0)
{
return (j - i + 1);
}
// Stores middle element
// of the subarray arr[i..j]
int mid = (i + j) / 2;
// Recursively partition the current
// subarray arr[i..j] into equal halves
int X = partitionsArr(arr, i, mid);
int Y = partitionsArr(arr, mid + 1, j);
return Math.max(X, Y);
}
// Driver Code
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));
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement
# the above approach
# Function to check if the subarray
# arr[i..j] is a sorted subarray or not
def isSortedparitions(arr, i, j):
# Traverse all elements of
# the subarray arr[i...j]
for k in range(i + 1, j + 1):
# If the previous element of the
# subarray exceeds current element
if (arr[k] < arr[k - 1]):
# Return 0
return 0
# Return 1
return 1
# Recursively partition the array
# into two equal halves
def partitionsArr(arr, i, j):
# If atmost one element is left
# in the subarray arr[i..j]
if (i >= j):
return 1
# Checks if subarray arr[i..j] is
# a sorted subarray or not
flag = int(isSortedparitions(arr, i, j))
# If the subarray arr[i...j]
# is a sorted subarray
if (flag != 0):
return (j - i + 1)
# Stores middle element
# of the subarray arr[i..j]
mid = (i + j) // 2
# Recursively partition the current
# subarray arr[i..j] into equal halves
X = partitionsArr(arr, i, mid);
Y = partitionsArr(arr, mid + 1, j)
return max(X, Y)
# Driver Code
if __name__ == '__main__':
arr = [ 11, 12, 1, 2, 13, 14, 3, 4 ]
N = len(arr)
print(partitionsArr(arr, 0, N - 1))
# This code is contributed by Princi Singh
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check if the subarray
// arr[i..j] is a sorted subarray or not
static int isSortedparitions(int[] arr,
int i, int j)
{
// Traverse all elements of
// the subarray arr[i...j]
for(int k = i + 1; k <= j; k++)
{
// If the previous element of the
// subarray exceeds current element
if (arr[k] < arr[k - 1])
{
// Return 0
return 0;
}
}
// Return 1
return 1;
}
// Recursively partition the array
// into two equal halves
static int partitionsArr(int[] arr, int i,
int j)
{
// If atmost one element is left
// in the subarray arr[i..j]
if (i >= j)
return 1;
// Checks if subarray arr[i..j] is
// a sorted subarray or not
int flag = (int)isSortedparitions(arr, i, j);
// If the subarray arr[i...j]
// is a sorted subarray
if (flag != 0)
{
return (j - i + 1);
}
// Stores middle element
// of the subarray arr[i..j]
int mid = (i + j) / 2;
// Recursively partition the current
// subarray arr[i..j] into equal halves
int X = partitionsArr(arr, i, mid);
int Y = partitionsArr(arr, mid + 1, j);
return Math.Max(X, Y);
}
// Driver Code
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));
}
}
// This code is contributed by code_hunt
Javascript
输出:
2
时间复杂度: O(N * log(N))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。