检查给定的数组是否是单调的
给定一个包含N个整数的数组arr[] ,任务是检查数组是否是单调的(单调意味着数组是升序还是降序)。
例子:
Input: arr[] = {1, 2, 2, 3}
Output: Yes
Explanation: Here 1 < 2 <= 2 < 3.
The array is in increasing order. Therefore it is monotonic.
Input: arr[] = {6, 5, 4, 3}
Output: Yes
Explanation: Here 6 > 5 > 4 > 3.
The array is in decreasing order. So it is monotonic.
Input: arr[] = {1, 5, 2}
Output: No
Explanation: Here 1 < 5 > 2. The array is neither increasing nor decreasing.
So the array is not monotonic
方法:可以通过检查数组是升序还是降序来解决问题。这可以通过以下方式轻松完成:
- If for each i in range [0, N-2], arr[i] ≥ arr[i+1] the array is in decreasing order.
- If for each i in range [0, N-2], arr[i] ≤ arr[i+1], the array is in increasing order.
请按照以下步骤解决问题:
- 遍历数组arr[]从i = 0 到 N-2并检查数组是否按顺序递增
- 遍历数组arr[]从i = 0 到 N-2并检查数组是否按顺序递减
- 如果以上两个都不为真,则数组不是单调的。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to check array is monotonic
bool check(vector& arr)
{
int N = arr.size();
bool inc = true;
bool dec = true;
// Loop to check if array is increasing
for (int i = 0; i < N - 1; i++) {
// To check if
// array is not increasing
if (arr[i] > arr[i + 1]) {
inc = false;
}
}
// Loop to check if array is decreasing
for (int i = 0; i < N - 1; i++) {
// To check if
// array is not decreasing
if (arr[i] < arr[i + 1]) {
dec = false;
}
}
// Pick one whether inc or dec
return inc || dec;
}
// Driver code
int main()
{
vector arr = { 1, 2, 3, 3 };
// Function call
bool ans = check(arr);
if (ans)
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for above approach
import java.io.*;
class GFG {
// Function to check array is monotonic
static boolean check(int arr[])
{
int N = arr.length;
boolean inc = true;
boolean dec = true;
// Loop to check if array is increasing
for (int i = 0; i < N - 1; i++) {
// To check if
// array is not increasing
if (arr[i] > arr[i + 1]) {
inc = false;
}
}
// Loop to check if array is decreasing
for (int i = 0; i < N - 1; i++) {
// To check if
// array is not decreasing
if (arr[i] < arr[i + 1]) {
dec = false;
}
}
// Pick one whether inc or dec
return inc || dec;
}
// Driver code
public static void main (String[] args) {
int arr[] = { 1, 2, 3, 3 };
// Function call
boolean ans = check(arr);
if (ans == true)
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python program for above approach
# Function to check array is monotonic
def check(arr):
N = len(arr)
inc = True
dec = True
# Loop to check if array is increasing
for i in range(0, N-1):
# To check if array is not increasing
if arr[i] > arr[i+1]:
inc = False
# Loop to check if array is decreasing
for i in range(0, N-1):
# To check if array is not decreasing
if arr[i] < arr[i+1]:
dec = False
# Pick one whether inc or dec
return inc or dec
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 3]
# Function call
ans = check(arr)
if ans == True:
print("Yes")
else:
print("No")
# This code is contributed by Rohit Pradhan
C#
// C# program for above approach
using System;
class GFG {
// Function to check array is monotonic
static bool check(int[] arr)
{
int N = arr.Length;
bool inc = true;
bool dec = true;
// Loop to check if array is increasing
for (int i = 0; i < N - 1; i++) {
// To check if
// array is not increasing
if (arr[i] > arr[i + 1]) {
inc = false;
}
}
// Loop to check if array is decreasing
for (int i = 0; i < N - 1; i++) {
// To check if
// array is not decreasing
if (arr[i] < arr[i + 1]) {
dec = false;
}
}
// Pick one whether inc or dec
return (inc || dec);
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 3, 3 };
// Function call
bool ans = check(arr);
if (ans)
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Yes
时间复杂度: O(N)
辅助空间: O(1)