Largest and smallest Fibonacci numbers in an Array
Given an array arr[] of N positive integers, the task is to find the minimum (smallest) and maximum (largest) Fibonacci elements in the given array.
Examples:
Input: arr[] = 1, 2, 3, 4, 5, 6, 7
Output: 1, 5
Explanation :
The array contains 4 fibonacci values 1, 2, 3 and 5.
Hence, the maximum is 5 and the minimum is 1.
Input: arr[] = 13, 3, 15, 6, 8, 11
Output:3, 13
Explanation:
The array contains 3 fibonacci values 13, 3 and 8.
Hence, the maximum is 13 and the minimum is 3.
Approach: This approach is similar to finding the minimum and maximum element in an array. Traverse the array one by one, and check if it is a Fibonacci number or not. If it is, then find the maximum and minimum among such numbers.
Inorder to check if the number is a Fibonacci number or not optimally O(1), generate all Fibonacci numbers up to the maximum element of the array using dynamic programming and store them in a hash table.
Below is the implementation of above approach:
C++
// C++ program to find minimum and maximum // fibonacci number in given array #include <bits/stdc++.h> using namespace std; // Function to create hash table // to check Fibonacci numbers void createHash(set<int>& hash, int maxElement) { // Insert initial two numbers // in the hash table int prev = 0, curr = 1; hash.insert(prev); hash.insert(curr); while (curr <= maxElement) { // Sum of previous two numbers int temp = curr + prev; hash.insert(temp); // Update the variable each time prev = curr; curr = temp; } } // Function to find minimum and maximum // fibonacci number in given array void fibonacci(int arr[], int n) { // Find maximum value in the array int max_val = *max_element( arr, arr + n); // Creating a set containing // all Fibonacci numbers up to // maximum value in the array set<int> hash; createHash(hash, max_val); // For storing the Minimum // and Maximum Fibonacci number int minimum = INT_MAX; int maximum = INT_MIN; for (int i = 0; i < n; i++) { // Check if current element // is a fibonacci number if (hash.find(arr[i]) != hash.end()) { // Update the maximum and // minimum accordingly minimum = min(minimum, arr[i]); maximum = max(maximum, arr[i]); } } cout << minimum << ", " << maximum << endl; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int n = sizeof(arr) / sizeof(arr[0]); fibonacci(arr, n); return 0; } |
Java
// Java program to find minimum and maximum // fibonacci number in given array import java.util.*; class GFG{ // Function to create hash table // to check Fibonacci numbers static void createHash(HashSet<Integer> hash, int maxElement) { // Insert initial two numbers // in the hash table int prev = 0, curr = 1; hash.add(prev); hash.add(curr); while (curr <= maxElement) { // Sum of previous two numbers int temp = curr + prev; hash.add(temp); // Update the variable each time prev = curr; curr = temp; } } // Function to find minimum and maximum // fibonacci number in given array static void fibonacci(int arr[], int n) { // Find maximum value in the array int max_val= Arrays.stream(arr).max().getAsInt(); // Creating a set containing // all Fibonacci numbers up to // maximum value in the array HashSet<Integer> hash = new HashSet<Integer>(); createHash(hash, max_val); // For storing the Minimum // and Maximum Fibonacci number int minimum = Integer.MAX_VALUE; int maximum = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { // Check if current element // is a fibonacci number if (hash.contains(arr[i])) { // Update the maximum and // minimum accordingly minimum = Math.min(minimum, arr[i]); maximum = Math.max(maximum, arr[i]); } } System.out.print(minimum+ ", " + maximum +"\n"); } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int n = arr.length; fibonacci(arr, n); } } // This code is contributed by sapnasingh4991 |
Python3
# Python 3 program to find minimum and maximum # fibonacci number in given array import sys # Function to create hash table # to check Fibonacci numbers def createHash(hash, maxElement): # Insert initial two numbers # in the hash table prev = 0 curr = 1 hash.add(prev) hash.add(curr) while (curr <= maxElement): # Sum of previous two numbers temp = curr + prev hash.add(temp) # Update the variable each time prev = curr curr = temp # Function to find minimum and maximum # fibonacci number in given array def fibonacci(arr, n): # Find maximum value in the array max_val = max(arr) # Creating a set containing # all Fibonacci numbers up to # maximum value in the array hash = set() createHash(hash, max_val) # For storing the Minimum # and Maximum Fibonacci number minimum = sys.maxsize maximum = -sys.maxsize-1 for i in range(n): # Check if current element # is a fibonacci number if (arr[i] in hash): # Update the maximum and # minimum accordingly minimum = min(minimum, arr[i]) maximum = max(maximum, arr[i]) print(minimum,end = ", ") print(maximum) # Driver code if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6, 7] n = len(arr) fibonacci(arr, n) # This code is contributed by Surendra_Gangwar |
C#
// C# program to find minimum and maximum // fibonacci number in given array using System; using System.Linq; using System.Collections.Generic; class GFG{ // Function to create hash table // to check Fibonacci numbers static void createHash(HashSet<int> hash, int maxElement) { // Insert initial two numbers // in the hash table int prev = 0, curr = 1; hash.Add(prev); hash.Add(curr); while (curr <= maxElement) { // Sum of previous two numbers int temp = curr + prev; hash.Add(temp); // Update the variable each time prev = curr; curr = temp; } } // Function to find minimum and maximum // fibonacci number in given array static void fibonacci(int []arr, int n) { // Find maximum value in the array int max_val= arr.Max(); // Creating a set containing // all Fibonacci numbers up to // maximum value in the array HashSet<int> hash = new HashSet<int>(); createHash(hash, max_val); // For storing the Minimum // and Maximum Fibonacci number int minimum = int.MaxValue; int maximum = int.MinValue; for (int i = 0; i < n; i++) { // Check if current element // is a fibonacci number if (hash.Contains(arr[i])) { // Update the maximum and // minimum accordingly minimum = Math.Min(minimum, arr[i]); maximum = Math.Max(maximum, arr[i]); } } Console.Write(minimum+ ", " + maximum +"\n"); } // Driver code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4, 5, 6, 7 }; int n = arr.Length; fibonacci(arr, n); } } // This code is contributed by Princi Singh |
1, 5
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:
- Sum and product of K smallest and largest Fibonacci numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Sum and product of k smallest and k largest prime numbers in the array
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Largest subset whose all elements are Fibonacci numbers
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- Difference between the largest and the smallest primes in an array
- kth smallest/largest in a small range unsorted array
- k largest(or smallest) elements in an array | added Min Heap method
- Remove all the fibonacci numbers from the given array
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
- Length of longest subsequence of Fibonacci Numbers in an Array
- 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.

