给定一个由N 个整数组成的数组arr[] ,任务是找出该数组是否可以划分为 2 个子数组,使得第一个子数组严格递增,第二个子数组严格递减,反之亦然。如果给定的数组可以被分割,则打印“是”,否则打印“否” 。
例子:
Input: arr[] = {3, 1, -2, -2, -1, 3}
Output: Yes
Explanation:
First sub-array {3, 1, -2} which is strictly decreasing and second sub-array is {-2, 1, 3} is strictly increasing.
Input: arr[] = {1, 1, 2, 3, 4, 5}
Output: No
Explanation:
The entire array is increasing.
朴素的方法:朴素的想法是将数组在每个可能的索引处划分为两个子数组,并明确检查第一个子数组是否严格递增,第二个子数组是否严格递减,反之亦然。如果我们可以破坏任何子数组,则打印“是”,否则打印“否” 。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,遍历数组并检查严格递增的序列,然后检查严格递减的子序列,反之亦然。以下是步骤:
- 如果 arr[1] > arr[0],则检查严格增加然后严格减少为:
- 检查每个连续的对,直到在任何索引处 i arr[i + 1] 小于 arr[i]。
- 现在从索引 i + 1 检查每个连续的对检查 arr[i + 1] 是否小于 arr[i] 直到数组结束。如果在任何索引 i 处, arr[i] 小于 arr[i + 1],则中断循环。
- 如果我们在上面的步骤中到达最后,则打印“是”否则打印“否” 。
- 如果 arr[1] < arr[0],则检查严格减少然后严格增加为:
- 检查每个连续的对,直到在任何索引处 i arr[i + 1] 大于 arr[i]。
- 现在从索引 i + 1 检查每个连续对检查 arr[i + 1] 是否大于 arr[i] 直到数组结束。如果在任何索引 i 处, arr[i] 大于 arr[i + 1],则中断循环。
- 如果我们在上面的步骤中到达最后,则打印“是”否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the given array
// forms an increasing decreasing
// sequence or vice versa
bool canMake(int n, int ar[])
{
// Base Case
if (n == 1)
return true;
else {
// First subarray is
// stricly increasing
if (ar[0] < ar[1]) {
int i = 1;
// Check for strictly
// increasing condition
// & find the break point
while (i < n
&& ar[i - 1] < ar[i]) {
i++;
}
// Check for strictly
// decreasing condition
// & find the break point
while (i + 1 < n
&& ar[i] > ar[i + 1]) {
i++;
}
// If i is equal to
// length of array
if (i >= n - 1)
return true;
else
return false;
}
// First subarray is
// strictly Decreasing
else if (ar[0] > ar[1]) {
int i = 1;
// Check for strictly
// increasing condition
// & find the break point
while (i < n
&& ar[i - 1] > ar[i]) {
i++;
}
// Check for strictly
// increasing condition
// & find the break point
while (i + 1 < n
&& ar[i] < ar[i + 1]) {
i++;
}
// If i is equal to
// length of array - 1
if (i >= n - 1)
return true;
else
return false;
}
// Condition if ar[0] == ar[1]
else {
for (int i = 2; i < n; i++) {
if (ar[i - 1] <= ar[i])
return false;
}
return true;
}
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof arr / sizeof arr[0];
// Function Call
if (canMake(n, arr)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if the given array
// forms an increasing decreasing
// sequence or vice versa
static boolean canMake(int n, int ar[])
{
// Base Case
if (n == 1)
return true;
else
{
// First subarray is
// stricly increasing
if (ar[0] < ar[1])
{
int i = 1;
// Check for strictly
// increasing condition
// & find the break point
while (i < n && ar[i - 1] < ar[i])
{
i++;
}
// Check for strictly
// decreasing condition
// & find the break point
while (i + 1 < n && ar[i] > ar[i + 1])
{
i++;
}
// If i is equal to
// length of array
if (i >= n - 1)
return true;
else
return false;
}
// First subarray is
// strictly Decreasing
else if (ar[0] > ar[1])
{
int i = 1;
// Check for strictly
// increasing condition
// & find the break point
while (i < n && ar[i - 1] > ar[i])
{
i++;
}
// Check for strictly
// increasing condition
// & find the break point
while (i + 1 < n && ar[i] < ar[i + 1])
{
i++;
}
// If i is equal to
// length of array - 1
if (i >= n - 1)
return true;
else
return false;
}
// Condition if ar[0] == ar[1]
else
{
for (int i = 2; i < n; i++)
{
if (ar[i - 1] <= ar[i])
return false;
}
return true;
}
}
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 1, 2, 3, 4, 5 };
int n = arr.length;
// Function Call
if (!canMake(n, arr)) {
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by Rohit_ranjan
Python3
# Python3 program for the above approach
# Function to check if the given array
# forms an increasing decreasing
# sequence or vice versa
def canMake(n, ar):
# Base Case
if (n == 1):
return True;
else:
# First subarray is
# stricly increasing
if (ar[0] < ar[1]):
i = 1;
# Check for strictly
# increasing condition
# & find the break point
while (i < n and ar[i - 1] < ar[i]):
i += 1;
# Check for strictly
# decreasing condition
# & find the break point
while (i + 1 < n and ar[i] > ar[i + 1]):
i += 1;
# If i is equal to
# length of array
if (i >= n - 1):
return True;
else:
return False;
# First subarray is
# strictly Decreasing
elif (ar[0] > ar[1]):
i = 1;
# Check for strictly
# increasing condition
# & find the break point
while (i < n and ar[i - 1] > ar[i]):
i += 1;
# Check for strictly
# increasing condition
# & find the break point
while (i + 1 < n and ar[i] < ar[i + 1]):
i += 1;
# If i is equal to
# length of array - 1
if (i >= n - 1):
return True;
else:
return False;
# Condition if ar[0] == ar[1]
else:
for i in range(2, n):
if (ar[i - 1] <= ar[i]):
return False;
return True;
# Driver Code
# Given array arr
arr = [1, 2, 3, 4, 5];
n = len(arr);
# Function Call
if (canMake(n, arr)==False):
print("Yes");
else:
print("No");
# This code is contributed by PrinciRaj1992
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the given array
// forms an increasing decreasing
// sequence or vice versa
static bool canMake(int n, int []ar)
{
// Base Case
if (n == 1)
return true;
else
{
// First subarray is
// stricly increasing
if (ar[0] < ar[1])
{
int i = 1;
// Check for strictly
// increasing condition
// & find the break point
while (i < n && ar[i - 1] < ar[i])
{
i++;
}
// Check for strictly
// decreasing condition
// & find the break point
while (i + 1 < n && ar[i] > ar[i + 1])
{
i++;
}
// If i is equal to
// length of array
if (i >= n - 1)
return true;
else
return false;
}
// First subarray is
// strictly Decreasing
else if (ar[0] > ar[1])
{
int i = 1;
// Check for strictly
// increasing condition
// & find the break point
while (i < n && ar[i - 1] > ar[i])
{
i++;
}
// Check for strictly
// increasing condition
// & find the break point
while (i + 1 < n && ar[i] < ar[i + 1])
{
i++;
}
// If i is equal to
// length of array - 1
if (i >= n - 1)
return true;
else
return false;
}
// Condition if ar[0] == ar[1]
else
{
for (int i = 2; i < n; i++)
{
if (ar[i - 1] <= ar[i])
return false;
}
return true;
}
}
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
// Function Call
if (!canMake(n, arr))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
No
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。