给定一个由N 个整数组成的数组arr[] ,任务是计算给定数组中反向双音子数组的总数。
A Reverse Bitonic Subarray is a subarray in which elements are arranged in decreasing order and then arranged in increasing order. A strictly increasing or strictly decreasing subarray is also considered as Reverse Bitonic Subarray.
例子:
Input: arr[] = {2, 3, 1, 4}
Output: 8
Explanation:
Here we will look for all length’s subarrays of given array
For length 1, all the subarrays are reverse bitonic subarray {2}, {3}, {1}, {4}
For length 2, possible subarrays are {2, 3}, {3, 1}, {1, 4}
For length 3, possible subarray is {3, 1, 4}
So in total, there are 8 subarrays possible.
Input: arr[] = [1, 2, 3]
Output: 6
Explanation:
Here we will look for all length’s subarrays of given array
For length 1, all the subarrays are reverse bitonic {1}, {2}, {3}
For length 2, possible subarrays are {1, 2}, {2, 3}
For length 3, possible subarray is {1, 2, 3}.
So in total, there are 6 subarrays possible.
方法:想法是从给定数组生成所有子数组,并检查每个子数组是否满足以下提到的条件:
- 当子数组元素严格递增时,取第一个元素,然后检查下一个元素是否递增。
- 当子数组元素严格递减时,取第一个元素,然后检查下一个元素是否递减。
- 当子数组元素严格减少然后增加时,在这种情况下,取第一个元素,然后检查下一个减少,当它变为假时,然后检查增加到最后一个元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that counts all the reverse
// bitonic subarray in arr[]
void countReversebitonic(int arr[],
int n)
{
// To store the count of reverse
// bitonic subarray
int c = 0;
// Iterate the array and select
// the starting element
for (int i = 0; i < n; i++) {
// Iterate for selecting the
// ending element for subarray
for (int j = i; j < n; j++) {
// Subarray arr[i to j]
int temp = arr[i], f = 0;
// For 1 length, increment
// the count and continue
if (j == i) {
c++;
continue;
}
int k = i + 1;
// For Decreasing Subarray
while (temp > arr[k]
&& k <= j) {
temp = arr[k];
k++;
}
// Check if only Decreasing
if (k > j) {
c++;
f = 2;
}
// For Increasing Subarray
while (temp < arr[k]
&& k <= j && f != 2) {
temp = arr[k];
k++;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
// Print the total count of subarrays
cout << c << endl;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 2, 3, 1, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countReversebitonic(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that counts all the reverse
// bitonic subarray in arr[]
static void countReversebitonic(int arr[],
int n)
{
// To store the count of reverse
// bitonic subarray
int c = 0;
// Iterate the array and select
// the starting element
for(int i = 0; i < n; i++)
{
// Iterate for selecting the
// ending element for subarray
for(int j = i; j < n; j++)
{
// Subarray arr[i to j]
int temp = arr[i], f = 0;
// For 1 length, increment
// the count and continue
if (j == i)
{
c++;
continue;
}
int k = i + 1;
// For decreasing subarray
while (temp > arr[k] && k <= j)
{
temp = arr[k];
k++;
}
// Check if only decreasing
if (k > j)
{
c++;
f = 2;
}
// For increasing subarray
while (k <= j && temp < arr[k] &&
f != 2)
{
temp = arr[k];
k++;
}
if (k > j && f != 2)
{
c++;
f = 0;
}
}
}
// Print the total count of subarrays
System.out.print(c + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 2, 3, 1, 4 };
int N = arr.length;
// Function Call
countReversebitonic(arr, N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function that counts all the reverse
# bitonic subarray in arr[]
def countReversebitonic(arr, n):
# To store the count of reverse
# bitonic subarray
c = 0;
# Iterate the array and select
# the starting element
for i in range(n):
# Iterate for selecting the
# ending element for subarray
for j in range(i, n):
# Subarray arr[i to j]
temp = arr[i]
f = 0;
# For 1 length, increment
# the count and continue
if (j == i):
c += 1;
continue;
k = i + 1;
# For Decreasing Subarray
while (k <= j and temp > arr[k]):
temp = arr[k];
k += 1;
# Check if only Decreasing
if (k > j):
c += 1;
f = 2;
# For Increasing Subarray
while (k <= j and temp < arr[k] and
f != 2):
temp = arr[k];
k += 1;
if (k > j and f != 2):
c += 1;
f = 0;
# Print the total count of subarrays
print(c)
# Driver Code
# Given array arr[]
arr = [ 2, 3, 1, 4 ];
# Function Call
countReversebitonic(arr, len(arr));
# This code is contributed by grand_master
C#
// C# program for the above approach
using System;
class GFG{
// Function that counts all the reverse
// bitonic subarray in arr[]
static void countReversebitonic(int []arr,
int n)
{
// To store the count of reverse
// bitonic subarray
int c = 0;
// Iterate the array and select
// the starting element
for(int i = 0; i < n; i++)
{
// Iterate for selecting the
// ending element for subarray
for(int j = i; j < n; j++)
{
// Subarray arr[i to j]
int temp = arr[i], f = 0;
// For 1 length, increment
// the count and continue
if (j == i)
{
c++;
continue;
}
int k = i + 1;
// For decreasing subarray
while (temp > arr[k] && k <= j)
{
temp = arr[k];
k++;
}
// Check if only decreasing
if (k > j)
{
c++;
f = 2;
}
// For increasing subarray
while (k <= j && temp < arr[k] &&
f != 2)
{
temp = arr[k];
k++;
}
if (k > j && f != 2)
{
c++;
f = 0;
}
}
}
// Print the total count of subarrays
Console.Write(c);
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
int []arr = { 2, 3, 1, 4 };
int N = arr.Length;
// Function Call
countReversebitonic(arr, N);
}
}
// This code is contributed by Ritik Bansal
Javascript
8
时间复杂度: O(N 2 ) ,其中 N 是给定数组中的元素数。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。