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

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

给定一个由N个整数组成的数组arr [] ,任务是计算本质上是Bitonic的所有子数组。

例子:

方法:
请按照以下步骤解决问题:

  1. 生成所有可能的子数组。
  2. 对于每个子阵列,检查其是否为双峰的。如果子阵列是Bitonic,则递增计数以获得答案。
  3. 最后返回答案。

下面是上述方法的实现:

C++
// C++ program to count the
// number of possible
// bitonic subarrays
#include 
using namespace std;
  
// Function to return the count
// of bitonic subarrays
void countbitonic(int arr[], int n)
{
    int c = 0;
    // Starting element of subarray
    for (int i = 0; i < n; i++) {
        // Ending element of subarray
        for (int j = i; j < n; j++) {
  
            int temp = arr[i], f = 0;
  
            // for 1 length
            if (j == i) {
                c++;
                continue;
            }
  
            int k = i + 1;
  
            // For increasing sequence
            while (temp < arr[k] && k <= j) {
                temp = arr[k];
                k++;
            }
  
            // If strictly increasing
            if (k > j) {
                c++;
                f = 2;
            }
  
            // For decreasing sequence
            while (temp > arr[k]
                   && k <= j
                   && f != 2) {
                temp = arr[k];
                k++;
            }
  
            if (k > j && f != 2) {
                c++;
                f = 0;
            }
        }
    }
  
    cout << c << endl;
}
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4, 3, 6, 5 };
    int N = 6;
  
    countbitonic(arr, N);
}


Java
// Java program to count the number 
// of possible bitonic subarrays
import java.io.*; 
import java.util.*; 
  
class GFG{ 
      
// Function to return the count 
// of bitonic subarrays 
public static void countbitonic(int arr[], int n)
{
    int c = 0;
      
    // Starting element of subarray
    for(int i = 0; i < n; i++) 
    {
          
       // Ending element of subarray
       for(int j = i; j < n; j++) 
       {
          int temp = arr[i], f = 0;
            
          // For 1 length
          if (j == i)
          {
              c++;
              continue;
          }
          int k = i + 1;
            
          // For increasing sequence
          while (temp < arr[k] && k <= j)
          {
              temp = arr[k];
              k++;
          }
            
          // If strictly increasing
          if (k > j) 
          {
              c++;
              f = 2;
          }
            
          // For decreasing sequence
          while ( k <= j && temp > arr[k] && f != 2) 
          {
              temp = arr[k];
              k++;
          }
          if (k > j && f != 2)
          {
              c++;
              f = 0;
          }
       }
    }
    System.out.println(c);
}
  
// Driver code
public static void main(String[] args) 
{ 
    int arr[] = { 1, 2, 4, 3, 6, 5 };
    int N = 6;
      
    countbitonic(arr, N);
} 
} 
  
// This code is contributed by grand_master


Python3
# Python3 program to count the number 
# of possible bitonic subarrays
  
# Function to return the count 
# of bitonic subarrays 
def countbitonic(arr, n):
  
    c = 0;
      
    # Starting element of subarray
    for i in range(n):
          
        # Ending element of subarray
        for j in range(i, n): 
            temp = arr[i]
            f = 0;
  
            # For 1 length
            if (j == i) :
                c += 1
                continue;
            k = i + 1;
  
            # For increasing sequence
            while (temp < arr[k] and k <= j):
                temp = arr[k];
                k += 1
                  
            # If strictly increasing
            if (k > j) :
                c += 1
                f = 2;
                  
            # For decreasing sequence
            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(c)
      
# Driver Code
arr = [ 1, 2, 4, 3, 6, 5 ];
N = 6;
  
countbitonic(arr, N);
  
# This code is contributed by grand_master


C#
// C# program to count the number 
// of possible bitonic subarrays 
using System;
  
class GFG{ 
  
// Function to return the count 
// of bitonic subarrays     
public static void countbitonic(int []arr, int n)
{
    int c = 0;
      
    // Starting element of subarray
    for(int i = 0; i < n; i++) 
    {
          
       // Ending element of subarray
       for(int j = i; j < n; j++)
       {
          int temp = arr[i], f = 0;
            
          // for 1 length
          if (j == i)
          {
              c++;
              continue;
          }
          int k = i + 1;
            
          // For increasing sequence
          while (temp < arr[k] && k <= j)
          {
              temp = arr[k];
              k++;
          }
            
          // If strictly increasing
          if (k > j)
          {
              c++;
              f = 2;
          }
            
          // For decreasing sequence
          while ( k <= j && temp > arr[k] && f != 2)
          {
              temp = arr[k];
              k++;
          }
          if (k > j && f != 2)
          {
              c++;
              f = 0;
          } 
       }
    }
    Console.Write(c);
}
  
// Driver code 
public static void Main() 
{ 
    int[] arr = { 1, 2, 4, 3, 6, 5 };
    int N = 6;
      
    countbitonic(arr, N);
} 
} 
  
// This code is contributed by grand_master


输出:
15

时间复杂度: O(N 3 )
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。