Count of total subarrays whose sum is a Fibonacci Numbers
Given an array arr[] of N integers, the task is to count total number of subarrays whose sum is a Fibonacci Number.
Examples:
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)
Approach: The idea is generate all possible subarray and find the sum of each subarray. Now for each sum check whether it is fibonacci or not by using the approach discussed in this article. If Yes then, count all those sum and print the total count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int x)
{
int s = sqrt (x);
return (s * s == x);
}
bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
void fibonacciSubarrays( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; ++i) {
int sum = 0;
for ( int j = i; j < n; ++j) {
sum += arr[j];
if (isFibonacci(sum)) {
++count;
}
}
}
cout << count;
}
int main()
{
int arr[] = { 6, 7, 8, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
fibonacciSubarrays(arr, n);
return 0;
}
|
Java
class GFG{
static boolean isPerfectSquare( int x)
{
int s = ( int ) Math.sqrt(x);
return (s * s == x);
}
static boolean isFibonacci( int n)
{
return isPerfectSquare( 5 * n * n + 4 )
|| isPerfectSquare( 5 * n * n - 4 );
}
static void fibonacciSubarrays( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; ++i) {
int sum = 0 ;
for ( int j = i; j < n; ++j) {
sum += arr[j];
if (isFibonacci(sum)) {
++count;
}
}
}
System.out.print(count);
}
public static void main(String[] args)
{
int arr[] = { 6 , 7 , 8 , 9 };
int n = arr.length;
fibonacciSubarrays(arr, n);
}
}
|
Python3
import math
def isPerfectSquare(x):
s = int (math.sqrt(x))
if s * s = = x:
return True
return False
def isFibonacci(n):
return (isPerfectSquare( 5 * n * n + 4 ) or
isPerfectSquare( 5 * n * n - 4 ))
def fibonacciSubarrays(arr, n):
count = 0
for i in range (n):
sum = 0
for j in range (i, n):
sum + = arr[j]
if (isFibonacci( sum )):
count + = 1
print (count)
arr = [ 6 , 7 , 8 , 9 ]
n = len (arr)
fibonacciSubarrays(arr, n)
|
C#
using System;
class GFG{
static bool isPerfectSquare( int x)
{
int s = ( int ) Math.Sqrt(x);
return (s * s == x);
}
static bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
static void fibonacciSubarrays( int []arr, int n)
{
int count = 0;
for ( int i = 0; i < n; ++i)
{
int sum = 0;
for ( int j = i; j < n; ++j)
{
sum += arr[j];
if (isFibonacci(sum))
{
++count;
}
}
}
Console.Write(count);
}
public static void Main(String[] args)
{
int []arr = { 6, 7, 8, 9 };
int n = arr.Length;
fibonacciSubarrays(arr, n);
}
}
|
Javascript
<script>
function isPerfectSquare(x)
{
var s = parseInt(Math.sqrt(x));
return (s * s == x);
}
function isFibonacci(n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
function fibonacciSubarrays(arr, n)
{
var count = 0;
for ( var i = 0; i < n; ++i) {
var sum = 0;
for ( var j = i; j < n; ++j) {
sum += arr[j];
if (isFibonacci(sum)) {
++count;
}
}
}
document.write( count);
}
var arr = [ 6, 7, 8, 9 ];
var n = arr.length;
fibonacciSubarrays(arr, n);
</script>
|
Time Complexity: O(N2), where N is the number of elements.
Auxiliary Space: O(1)
Last Updated :
15 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...