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++
// 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; } |
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 |
3
Time Complexity: O(N2), where N is the number of elements.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Count subarrays having total distinct elements same as original array
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Count total set bits in all numbers from 1 to n | Set 2
- Count of subarrays having exactly K prime numbers
- Count total unset bits in all the numbers from 1 to N
- Count of subarrays having exactly K perfect square numbers
- Count subarrays with sum as difference of squares of two numbers
- Find the total count of numbers up to N digits in a given base B
- Count of subarrays whose product is equal to difference of two different numbers
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count non decreasing subarrays of size N from N Natural numbers
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Array range queries to count the number of Fibonacci numbers with updates
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.
Improved By : princiraj1992

