📌  相关文章
📜  计数至少大小为 3 的子阵列,形成几何级数 (GP)

📅  最后修改于: 2021-09-07 02:13:18             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是从给定的数组中找到所有子数组的计数,该数组的大小至少为3,形成几何级数。

例子:

朴素的方法:最简单的方法是生成大小至少为 3 的所有子阵列,并计算所有形成几何级数的子阵列。检查所有子数组后打印计数。

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

有效的方法:这个想法是使用几何级数的属性,即{a, b, c}是 GP 当且仅当a*c = b 。请按照以下步骤解决问题:

  • 初始化一个变量res ,并用0计数来存储构成当前子数组的几何级数和长度的总子数组。
  • [2, N – 1]范围内遍历给定数组,如果当前元素形成几何级数,则增加count的值,即arr[i]*arr[i – 2] = arr[i – 1]*arr [i – 1]否则,将计数设置为零。
  • 在上述步骤中为每次迭代添加计数res
  • 完成上述步骤后,打印res的值作为结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count all the subarrays
// of size at least 3 forming GP
int numberOfGP(int L[], int N)
{
    // If array size is less than 3
    if (N <= 2)
        return 0;
 
    // Stores the count of subarray
    int count = 0;
 
    // Stores the count of subarray
    // for each iteration
    int res = 0;
 
    // Traverse the array
    for (int i = 2; i < N; ++i) {
 
        // Check if L[i] forms GP
        if (L[i - 1] * L[i - 1]
            == L[i] * L[i - 2]) {
            ++count;
        }
 
        // Otherwise, update count to 0
        else {
            count = 0;
        }
 
        // Update the final count
        res += count;
    }
 
    // Return the final count
    return res;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 4, 8, 16, 24 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << numberOfGP(arr, N);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to count all
// the subarrays of size
// at least 3 forming GP
static int numberOfGP(int L[],
                      int N)
{
  // If array size
  // is less than 3
  if (N <= 2)
    return 0;
 
  // Stores the count
  // of subarray
  int count = 0;
 
  // Stores the count
  // of subarray for
  // each iteration
  int res = 0;
 
  // Traverse the array
  for (int i = 2; i < N; ++i)
  {
    // Check if L[i] forms GP
    if (L[i - 1] * L[i - 1] ==
        L[i] * L[i - 2])
    {
      ++count;
    }
 
    // Otherwise, update
    // count to 0
    else
    {
      count = 0;
    }
 
    // Update the
    // final count
    res += count;
  }
 
  // Return the final count
  return res;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int arr[] = {1, 2, 4,
               8, 16, 24};
 
  int N = arr.length;
 
  // Function Call
  System.out.print(numberOfGP(arr, N));
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function to count all the subarrays
# of size at least 3 forming GP
def numberOfGP(L, N):
     
    # If array size is less than 3
    if (N <= 2):
        return 0
 
    # Stores the count of subarray
    count = 0
 
    # Stores the count of subarray
    # for each iteration
    res = 0
 
    # Traverse the array
    for i in range(2, N):
 
        # Check if L[i] forms GP
        if (L[i - 1] * L[i - 1] ==
                L[i] * L[i - 2]):
            count += 1
 
        # Otherwise, update count to 0
        else:
            count = 0
 
        # Update the final count
        res += count
 
    # Return the final count
    return res
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 1, 2, 4, 8, 16, 24 ]
 
    N = len(arr)
 
    # Function Call
    print(numberOfGP(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
class GFG {
 
// Function to count all
// the subarrays of size
// at least 3 forming GP
static int numberOfGP(int[] L,
                      int N)
{
  // If array size
  // is less than 3
  if (N <= 2)
    return 0;
 
  // Stores the count
  // of subarray
  int count = 0;
 
  // Stores the count
  // of subarray for
  // each iteration
  int res = 0;
 
  // Traverse the array
  for (int i = 2; i < N; ++i)
  {
    // Check if L[i] forms GP
    if (L[i - 1] * L[i - 1] ==
        L[i] * L[i - 2])
    {
      ++count;
    }
 
    // Otherwise, update
    // count to 0
    else
    {
      count = 0;
    }
 
    // Update the
    // final count
    res += count;
  }
 
  // Return the final
  // count
  return res;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array arr[]
  int[] arr = {1, 2, 4, 8, 16, 24};
 
  int N = arr.Length;
 
  // Function Call
  Console.Write(numberOfGP(arr, N));
}
}
 
// This code is contributed by Chitranayal


Javascript


输出
6

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live