检查数组是否是双调的程序
给定一个包含 N 个元素的数组。任务是检查阵列是否是双调的。
如果数组中的元素首先严格增加然后严格减少,则称该数组是双调的。
例子:
Input: arr[] = {-3, 9, 11, 20, 17, 5, 1}
Output: YES
Input: arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 11};
Output: NO
方法:
- 开始遍历数组并继续检查下一个元素是否大于当前元素。
- 如果在任何时候,下一个元素不大于当前元素,则中断循环。
- 再次从当前元素开始遍历并检查下一个元素是否小于当前元素。
- 如果在到达数组末尾之前的任何点,如果下一个元素不小于当前元素,则中断循环并打印 NO。
- 如果成功到达数组末尾,则打印 YES。
以下是上述方法的实现:
C++
// C++ program to check if an array is bitonic
#include
using namespace std;
// Function to check if the given array is bitonic
int checkBitonic(int arr[], int n)
{
int i, j;
// Check for increasing sequence
for (i = 1; i < n; i++) {
if (arr[i] > arr[i - 1])
continue;
if (arr[i] <= arr[i - 1])
break;
}
if (i > n - 1)
return 1;
// Check for decreasing sequence
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[j - 1])
continue;
if (arr[j] >= arr[j - 1])
break;
}
i = j;
if (i != n)
return 0;
return 1;
}
// Driver code
int main()
{
int arr[] = { 1,2,3 };
int n = sizeof(arr) / sizeof(arr[0]);
(checkBitonic(arr, n) == 1) ? cout << "YES"
: cout << "NO";
return 0;
}
Java
// Java program to check
// if an array is bitonic
class GFG
{
// Function to check if the
// given array is bitonic
static int checkBitonic(int arr[], int n)
{
int i, j;
// Check for increasing sequence
for (i = 1; i < n; i++)
{
if (arr[i] > arr[i - 1])
continue;
if (arr[i] <= arr[i - 1])
break;
}
if (i == n - 1)
return 1;
// Check for decreasing sequence
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[j - 1])
continue;
if (arr[j] >= arr[j - 1])
break;
}
i = j;
if (i != n)
return 0;
return 1;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { -3, 9, 7, 20, 17, 5, 1 };
int n = arr.length;
System.out.println((checkBitonic(arr, n) == 1) ?
"YES" : "NO");
}
}
// This code is contributed by Bilal
Python3
# Python3 program to check if
# an array is bitonic or not.
# Function to check if the
# given array is bitonic
def checkBitonic(arr, n) :
# Check for increasing sequence
for i in range(1, n) :
if arr[i] > arr[i - 1] :
continue
else :
break
if i == n-1 :
return 1
# Check for decreasing sequence
for j in range(i + 1, n) :
if arr[j] < arr[j - 1] :
continue
else :
break
i = j
if i != n - 1 :
return 0
return 1
# Driver Code
if __name__ == "__main__" :
arr = [-3, 9, 7, 20, 17, 5, 1]
n = len(arr)
if checkBitonic(arr, n) == 1 :
print("YES")
else :
print("NO")
# This code is contributed
# by ANKITRAI1
C#
// C# program to check
// if an array is bitonic
using System;
class GFG
{
// Function to check if the
// given array is bitonic
static int checkBitonic(int []arr,
int n)
{
int i, j;
// Check for increasing sequence
for (i = 1; i < n; i++)
{
if (arr[i] > arr[i - 1])
continue;
if (arr[i] <= arr[i - 1])
break;
}
if (i == n - 1)
return 1;
// Check for decreasing sequence
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[j - 1])
continue;
if (arr[j] >= arr[j - 1])
break;
}
i = j;
if (i != n)
return 0;
return 1;
}
// Driver Code
public static void Main()
{
int []arr = { -3, 9, 7, 20, 17, 5, 1 };
int n = arr.Length;
Console.WriteLine((
checkBitonic(arr, n) == 1) ?
"YES" : "NO");
}
}
// This code is contributed by Bilal
PHP
$arr[$i - 1])
continue;
if ($arr[$i] <= $arr[$i - 1])
break;
}
if ($i == $n - 1)
return 1;
// Check for decreasing sequence
for ($j = $i + 1; $j < $n; $j++)
{
if ($arr[$j] < $arr[$j - 1])
continue;
if ($arr[$j] >= $arr[$j - 1])
break;
}
$i = $j;
if ($i != $n)
return 0;
return 1;
}
// Driver code
$arr = array( -3, 9, 7, 20, 17, 5, 1 );
$n = sizeof($arr);
checkBitonic($arr, $n) == 1 ?
print("YES") : print("NO");
// This code is contributed by ChitraNayal
?>
Javascript
输出:
NO