Dada una array arr[] que consta de N enteros, la tarea es comprobar si se puede formar una serie de Fibonacci utilizando todos los elementos de la array o no. Si es posible, escriba «Sí». De lo contrario, escriba “No”.
Ejemplos:
Entrada: arr[] = { 8, 3, 5, 13 }
Salida: Sí
Explicación:
Reorganice la array dada como {3, 5, 8, 13} y estos números forman la serie de Fibonacci.
Entrada: arr[] = { 2, 3, 5, 11 }
Salida: No
Explicación:
Los elementos de array dados no forman una serie de Fibonacci.
Enfoque:
para resolver el problema mencionado anteriormente, la idea principal es ordenar la array dada. Después de ordenar, verifique si cada elemento es igual a la suma de los 2 elementos anteriores. Si es así, entonces los elementos de la array forman una serie de Fibonacci.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to check if the // elements of a given array // can form a Fibonacci Series #include <bits/stdc++.h> using namespace std; // Returns true if a permutation // of arr[0..n-1] can form a // Fibonacci Series bool checkIsFibonacci(int arr[], int n) { if (n == 1 || n == 2) return true; // Sort array sort(arr, arr + n); // After sorting, check if every // element is equal to the // sum of previous 2 elements for (int i = 2; i < n; i++) if ((arr[i - 1] + arr[i - 2]) != arr[i]) return false; return true; } // Driver Code int main() { int arr[] = { 8, 3, 5, 13 }; int n = sizeof(arr) / sizeof(arr[0]); if (checkIsFibonacci(arr, n)) cout << "Yes" << endl; else cout << "No"; return 0; }
Java
// Java program to check if the elements of // a given array can form a Fibonacci Series import java. util. Arrays; class GFG{ // Returns true if a permutation // of arr[0..n-1] can form a // Fibonacci Series public static boolean checkIsFibonacci(int arr[], int n) { if (n == 1 || n == 2) return true; // Sort array Arrays.sort(arr); // After sorting, check if every // element is equal to the sum // of previous 2 elements for(int i = 2; i < n; i++) { if ((arr[i - 1] + arr[i - 2]) != arr[i]) return false; } return true; } // Driver code public static void main(String[] args) { int arr[] = { 8, 3, 5, 13 }; int n = arr.length; if (checkIsFibonacci(arr, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by divyeshrabadiya07
Python3
# Python3 program to check if the # elements of a given array # can form a Fibonacci Series # Returns true if a permutation # of arr[0..n-1] can form a # Fibonacci Series def checkIsFibonacci(arr, n) : if (n == 1 or n == 2) : return True; # Sort array arr.sort() # After sorting, check if every # element is equal to the # sum of previous 2 elements for i in range(2, n) : if ((arr[i - 1] + arr[i - 2])!= arr[i]) : return False; return True; # Driver Code if __name__ == "__main__" : arr = [ 8, 3, 5, 13 ]; n = len(arr); if (checkIsFibonacci(arr, n)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01
C#
// C# program to check if the elements of // a given array can form a fibonacci series using System; class GFG{ // Returns true if a permutation // of arr[0..n-1] can form a // fibonacci series public static bool checkIsFibonacci(int []arr, int n) { if (n == 1 || n == 2) return true; // Sort array Array.Sort(arr); // After sorting, check if every // element is equal to the sum // of previous 2 elements for(int i = 2; i < n; i++) { if ((arr[i - 1] + arr[i - 2]) != arr[i]) return false; } return true; } // Driver code public static void Main(string[] args) { int []arr = { 8, 3, 5, 13 }; int n = arr.Length; if (checkIsFibonacci(arr, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript program to check if the elements of // a given array can form a Fibonacci Series // Returns true if a permutation // of arr[0..n-1] can form a // Fibonacci Series function checkIsFibonacci(arr , n) { if (n == 1 || n == 2) return true; // Sort array arr.sort((a, b) => a - b); // After sorting, check if every // element is equal to the sum // of previous 2 elements for (i = 2; i < n; i++) { if ((arr[i - 1] + arr[i - 2]) != arr[i]) return false; } return true; } // Driver code var arr = [ 8, 3, 5, 13 ]; var n = arr.length; if (checkIsFibonacci(arr, n)) document.write("Yes"); else document.write("No"); // This code contributed by umadevi9616 </script>
Yes
Complejidad de tiempo: O (N Log N)