给定N个整数的数组arr [] ,任务是从给定大小至少为3的构成几何级数的给定数组中查找所有子数组的计数。
例子:
Input: arr[] = {1, 2, 4, 8}
Output: 3
Explanation: The required subarrays forming geometric progression are:
- {1, 2, 4}
- {2, 4, 8}
- {1, 2, 4, 8}
Input: arr[] = {1, 2, 4, 8, 16, 24}
Output: 6
Explanation: The required subarrays forming geometric progression are:
- {1, 2, 4}
- {2, 4, 8}
- {4, 8, 16}
- {1, 2, 4, 8}
- {2, 4, 8, 16}
- {1, 2, 4, 8, 16}
天真的方法:最简单的方法是生成大小至少为3的所有子数组,并对构成几何级数的所有这些子数组进行计数。检查所有子数组后打印计数。
时间复杂度: O(N 3 )
辅助空间: O(N)
高效的方法:这个想法是使用几何级数的一个属性,即当且仅当a * c = b时, {a,b,c}是GP。请按照以下步骤解决问题:
- 初始化变量res ,并用0计数以存储形成当前子数组的几何级数和长度的总子数组。
- 如果当前元素形成几何级数,即arr [i] * arr [i – 2] = arr [i – 1] * arr,则遍历给定数组在[2,N – 1]范围内并增加count的值[i – 1]否则,将count设置为零。
- 在上述步骤中,将计数添加到每次迭代的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)