Given an array arr[], the task is to count the number of consecutive Fibonacci pairs in this array.
Examples:
Input: arr[] = { 3, 5, 6, 11 }
Output: 1
The only pair is (3, 5) which is consecutive fibonacci pair in the array
Input: arr[] = { 3, 5, 8, 11 }
Output: 2
There are two pairs (3, 5) and (5, 8) in the array, which is consecutive Fibonacci pair.
Approach:
- Find out all the pairs of the array.
- For each pair,
- Find the minimum and maximum of the pair
- Find the next consective fibonacci number after minimum_element and check that it is equal to the maximum of the pair.
- If the next consecutive fibonacci number is equal to the maximum element of the pair, then increment the count by 1.
- Return the total count as the required number of pairs.
Below is the implementation of the above approach:
C++
// C++ implementation to count the// consecutive fibonacci pairs in the array#include <bits/stdc++.h>using namespace std;// Function to find the previous// fibonacci for the number Nint previousFibonacci(int n){ double a = n / ((1 + sqrt(5)) / 2.0); return round(a);}// Function to find the next// fibonacci number for the number Nint nextFibonacci(int n){ double a = n * (1 + sqrt(5)) / 2.0; return round(a);}// Function to check that a Number// is a perfect square or notbool isPerfectSquare(int x){ int s = sqrt(x); return (s * s == x);}// Function to check that a number// is fibonacci number or notbool isFibonacci(int n){ // N is Fibinacci if one of // (5*n*n + 4) or (5*n*n - 4) // is a perferct square return (isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4));}// Function to count the fibonacci// pairs in the arrayint countFibonacciPairs(int arr[], int n){ int res = 0; // Loop to iterate over the array // to choose all pairs of the array for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) // Condition to check if both // the number of pair is a // fibonacci number if (isFibonacci(arr[i]) && isFibonacci(arr[j])) { int prevFib = previousFibonacci(arr[i]); int nextFib = nextFibonacci(arr[i]); // Condition to check if both // the number form consecutive // fibonacci numbers if (prevFib == arr[j] || nextFib == arr[j]) { res++; } } return res;}// Driver Codeint main(){ int a[] = { 3, 5, 8, 11 }; int n = sizeof(a) / sizeof(a[0]); cout << countFibonacciPairs(a, n); return 0;} |
Java
// Java implementation to count the// consecutive fibonacci pairs in the arrayimport java.util.*;import java.lang.*;class GFG{ // Function to find the previous// fibonacci for the number Nstatic int previousFibonacci(int n){ double a = n / ((1 + (int)Math.sqrt(5)) / 2.0); return (int)Math.round(a);} // Function to find the next// fibonacci number for the number Nstatic int nextFibonacci(int n){ double a = n * (1 + (int)Math.sqrt(5)) / 2.0; return (int)Math.round(a);} // Function to check that a Number// is a perfect square or notstatic boolean isPerfectSquare(int x){ int s = (int)Math.sqrt(x); return (s * s == x);} // Function to check that a number// is fibonacci number or notstatic boolean isFibonacci(int n){ // N is Fibinacci if one of // (5*n*n + 4) or (5*n*n - 4) // is a perferct square return (isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4));} // Function to count the fibonacci// pairs in the arraystatic int countFibonacciPairs(int arr[], int n){ int res = 0; // Loop to iterate over the array // to choose all pairs of the array for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) // Condition to check if both // the number of pair is a // fibonacci number if (isFibonacci(arr[i]) && isFibonacci(arr[j])) { int prevFib = previousFibonacci(arr[i]); int nextFib = nextFibonacci(arr[i]); // Condition to check if both // the number form consecutive // fibonacci numbers if (prevFib == arr[j] || nextFib == arr[j]) { res++; } } return res;} // Driver Codepublic static void main(String []args){ int []a = { 3, 5, 8, 11 }; int n = a.length; System.out.print(countFibonacciPairs(a, n)); }}// This code is contributed by chitranayal |
Python3
# Python3 implementation to count the# consecutive fibonacci pairs in the arrayfrom math import sqrt# Function to find the previous# fibonacci for the number Ndef previousFibonacci(n): a = n / ((1 + sqrt(5)) / 2.0) return round(a)# Function to find the next# fibonacci number for the number Ndef nextFibonacci(n): a = n * (1 + sqrt(5)) / 2.0 return round(a)# Function to check that a Number# is a perfect square or notdef isPerfectSquare(x): s = sqrt(x) return (s * s == x)# Function to check that a number# is fibonacci number or notdef isFibonacci(n): # N is Fibinacci if one of # (5*n*n + 4) or (5*n*n - 4) # is a perferct square return (isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4))# Function to count the fibonacci# pairs in the arraydef countFibonacciPairs(arr, n): res = 0 # Loop to iterate over the array # to choose all pairs of the array for i in range(n): for j in range(i+1,n): # Condition to check if both # the number of pair is a # fibonacci number if (isFibonacci(arr[i]) and isFibonacci(arr[j])): prevFib = previousFibonacci(arr[i]) nextFib = nextFibonacci(arr[i]) # Condition to check if both # the number form consecutive # fibonacci numbers if (prevFib == arr[j] or nextFib == arr[j]): res += 1 return res# Driver Codea = [3, 5, 8, 11]n = len(a)print(countFibonacciPairs(a, n))# This code is contributed by mohit kumar 29 |
C#
// C# implementation to count the// consecutive fibonacci pairs in the arrayusing System;class GFG{ // Function to find the previous// fibonacci for the number Nstatic int previousFibonacci(int n){ double a = n / ((1 + (int)Math.Sqrt(5)) / 2.0); return (int)Math.Round(a);} // Function to find the next// fibonacci number for the number Nstatic int nextFibonacci(int n){ double a = n * (1 + Math.Sqrt(5)) / 2.0; return (int)Math.Round(a);} // Function to check that a Number// is a perfect square or notstatic bool isPerfectSquare(int x){ int s = (int)Math.Sqrt(x); return (s * s == x);} // Function to check that a number// is fibonacci number or notstatic bool isFibonacci(int n){ // N is Fibinacci if one of // (5*n*n + 4) or (5*n*n - 4) // is a perferct square return (isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4));} // Function to count the fibonacci// pairs in the arraystatic int countFibonacciPairs(int []arr, int n){ int res = 0; // Loop to iterate over the array // to choose all pairs of the array for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) // Condition to check if both // the number of pair is a // fibonacci number if (isFibonacci(arr[i]) && isFibonacci(arr[j])) { int prevFib = previousFibonacci(arr[i]); int nextFib = nextFibonacci(arr[i]); // Condition to check if both // the number form consecutive // fibonacci numbers if (prevFib == arr[j] || nextFib == arr[j]) { res++; } } return res;} // Driver Codepublic static void Main(String []args){ int []a = { 3, 5, 8, 11 }; int n = a.Length; Console.Write(countFibonacciPairs(a, n)); }}// This code is contributed by sapnasingh4991 |
Javascript
<script>// Javascript implementation to count the// consecutive fibonacci pairs in the array// Function to find the previous// fibonacci for the number Nfunction previousFibonacci(n){ var a = n / ((1 + Math.sqrt(5)) / 2.0); return Math.round(a);}// Function to find the next// fibonacci number for the number Nfunction nextFibonacci(n){ var a = n * (1 + Math.sqrt(5)) / 2.0; return Math.round(a);}// Function to check that a Number// is a perfect square or notfunction isPerfectSquare(x){ var s = Math.sqrt(x); return (s * s == x);}// Function to check that a number// is fibonacci number or notfunction isFibonacci(n){ // N is Fibinacci if one of // (5*n*n + 4) or (5*n*n - 4) // is a perferct square return (isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4));}// Function to count the fibonacci// pairs in the arrayfunction countFibonacciPairs(arr, n){ var res = 0; // Loop to iterate over the array // to choose all pairs of the array for (var i = 0; i < n; i++) for (var j = i + 1; j < n; j++) // Condition to check if both // the number of pair is a // fibonacci number if (isFibonacci(arr[i]) && isFibonacci(arr[j])) { var prevFib = previousFibonacci(arr[i]); var nextFib = nextFibonacci(arr[i]); // Condition to check if both // the number form consecutive // fibonacci numbers if (prevFib == arr[j] || nextFib == arr[j]) { res++; } } return res;}// Driver Codevar a = [ 3, 5, 8, 11 ];var n = a.length;document.write(countFibonacciPairs(a, n));</script> |
2
Performance Analysis:
- Time Complexity: As in the above approach, there are two nested loops which takes O(N2) time, Hence the Time Complexity will be O(N2).
- Space Complexity: As in the above approach, there is no extra space used, Hence the space complexity will be O(1).
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


