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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

3


Time Complexity: O(N2)
, where N is the number of elements.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.