📜  所有可能的反向双音子阵列的计数

📅  最后修改于: 2021-10-26 06:05:39             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是计算给定数组中反向双音子数组的总数。

例子:

方法:想法是从给定数组生成所有子数组,并检查每个子数组是否满足以下提到的条件:

  • 当子数组元素严格递增时,取第一个元素,然后检查下一个元素是否递增。
  • 当子数组元素严格递减时,取第一个元素,然后检查下一个元素是否递减。
  • 当子数组元素严格减少然后增加时,在这种情况下,取第一个元素,然后检查下一个减少,当它变为假时,然后检查增加到最后一个元素。

下面是上述方法的实现:

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 现场工作专业课程学生竞争性编程现场课程