给定N个整数的数组arr [] ,任务是计算总和为斐波那契数的子数组的总数。
例子:
Input: arr[] = {6, 7, 8, 9}
Output: 3
Explanation:
The subarray whose sum is fibonacci numbers are:
1. {6, 7}, sum = 13 (5 + 8)
2. {6, 7, 8}, sum = 21 (8 + 13)
3. {8}, sum = 8 (3 + 5)
Input: arr[] = {1, 1, 1, 1}
Output: 4
Explanation:
The subarray whose sum is fibonacci numbers are:
1. {4, 2, 2}, sum = 8 (3 + 5)
2. {2}, sum = 2 (1 + 1)
3. {2}, sum = 2 (1 + 1)
4. {2}, sum = 2 (1 + 1)
方法:想法是生成所有可能的子数组并找到每个子数组的总和。现在,对于每个总和,使用本文中讨论的方法检查是否为斐波那契。如果是,则对所有这些总和进行计数并打印总计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether a number
// is perfect square or not
bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s * s == x);
}
// Function to check whether a number
// is fibonacci number or not
bool isFibonacci(int n)
{
// If 5*n*n + 4 or 5*n*n - 5 is a
// perfect square, then the number
// is Fibonacci
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
// Function to count the subarray with
// sum fibonacci number
void fibonacciSubarrays(int arr[], int n)
{
int count = 0;
// Traverse the array arr[] to find
// the sum of each subarray
for (int i = 0; i < n; ++i) {
// To store the sum
int sum = 0;
for (int j = i; j < n; ++j) {
sum += arr[j];
// Check whether sum of subarray
// between [i, j] is fibonacci
// or not
if (isFibonacci(sum)) {
++count;
}
}
}
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 6, 7, 8, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
fibonacciSubarrays(arr, n);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check whether a number
// is perfect square or not
static boolean isPerfectSquare(int x)
{
int s = (int) Math.sqrt(x);
return (s * s == x);
}
// Function to check whether a number
// is fibonacci number or not
static boolean isFibonacci(int n)
{
// If 5*n*n + 4 or 5*n*n - 5 is a
// perfect square, then the number
// is Fibonacci
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
// Function to count the subarray
// with sum fibonacci number
static void fibonacciSubarrays(int arr[], int n)
{
int count = 0;
// Traverse the array arr[] to find
// the sum of each subarray
for (int i = 0; i < n; ++i) {
// To store the sum
int sum = 0;
for (int j = i; j < n; ++j) {
sum += arr[j];
// Check whether sum of subarray
// between [i, j] is fibonacci
// or not
if (isFibonacci(sum)) {
++count;
}
}
}
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 6, 7, 8, 9 };
int n = arr.length;
// Function Call
fibonacciSubarrays(arr, n);
}
}
// This code contributed by PrinciRaj1992
Python3
# Python3 program for the above approach
import math
# Function to check whether a number
# is perfect square or not
def isPerfectSquare(x):
s = int(math.sqrt(x))
if s * s == x:
return True
return False
# Function to check whether a number
# is fibonacci number or not
def isFibonacci(n):
# If 5*n*n + 4 or 5*n*n - 5 is a
# perfect square, then the number
# is Fibonacci
return (isPerfectSquare(5 * n * n + 4) or
isPerfectSquare(5 * n * n - 4))
# Function to count the subarray with
# sum fibonacci number
def fibonacciSubarrays(arr, n):
count = 0
# Traverse the array arr[] to find
# the sum of each subarray
for i in range(n):
# To store the sum
sum = 0
for j in range(i, n):
sum += arr[j]
# Check whether sum of subarray
# between [i, j] is fibonacci
# or not
if (isFibonacci(sum)):
count += 1
print(count)
# Driver Code
arr = [ 6, 7, 8, 9 ]
n = len(arr)
# Function Call
fibonacciSubarrays(arr, n)
# This code is contributed by SHUBHAMSINGH10
C#
// C# program for the above approach
using System;
class GFG{
// Function to check whether a number
// is perfect square or not
static bool isPerfectSquare(int x)
{
int s = (int) Math.Sqrt(x);
return (s * s == x);
}
// Function to check whether a number
// is fibonacci number or not
static bool isFibonacci(int n)
{
// If 5*n*n + 4 or 5*n*n - 5 is a
// perfect square, then the number
// is Fibonacci
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
// Function to count the subarray
// with sum fibonacci number
static void fibonacciSubarrays(int []arr, int n)
{
int count = 0;
// Traverse the array []arr to find
// the sum of each subarray
for(int i = 0; i < n; ++i)
{
// To store the sum
int sum = 0;
for(int j = i; j < n; ++j)
{
sum += arr[j];
// Check whether sum of subarray
// between [i, j] is fibonacci
// or not
if (isFibonacci(sum))
{
++count;
}
}
}
Console.Write(count);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 6, 7, 8, 9 };
int n = arr.Length;
// Function Call
fibonacciSubarrays(arr, n);
}
}
// This code is contributed by Princi Singh
Javascript
输出:
3
时间复杂度: O(N 2 ) ,其中N是元素数。