给定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]
- 检查每对连续的对,直到在任何索引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
输出:
No
时间复杂度: O(N)
辅助空间: O(1)