Given an integer **K** and an array **arr[]** containing **N** integers, the task is to find the sum and product of **K** smallest and **K** largest fibonacci numbers in the array.

**Note:** Assume that there are at least **K** fibonacci numbers in the array.

**Examples:**

Input:arr[] = {2, 5, 6, 8, 10, 11}, K = 2

Output:

Sum of K-minimum fibonacci numbers is 7

Product of K-minimum fibonacci numbers is 10

Sum of K-maximum fibonacci numbers is 13

Product of K-maximum fibonacci numbers is 40

Explanation :

{2, 5, 8} are the only fibonacci numbers from the array.

{2, 5} are the 2 smallest and {5, 8} are the 2 largest among them.

Input:arr[] = {3, 2, 12, 13, 5, 19}, K = 3

Output:

Sum of K-minimum fibonacci numbers is 10

Product of K-minimum fibonacci numbers is 30

Sum of K-maximum fibonacci numbers is 21

Product of K-maximum fibonacci numbers is 195

**Approach:** The idea is to use hashing to precompute and store the Fibonacci nodes up to the maximum value, in a Set, to make checking easy and efficient (in O(1) time).

- Traverse through the entire array and obtain the maximum value in the list.
- Now, build a hash table containing all the Fibonacci nodes less than or equal to the maximum value of the array.

After performing the above precomputation, traverse the array and insert all the numbers which are fibonacci in two heaps, a *min heap and a max heap*.

Now, pop out top **K** elements from the min heap and max heap to compute the *sum* and *product* of the **K** Fibonacci numbers.

Below is the implementation of the above approach:

`// C++ program to find the sum and ` `// product of K smallest and K ` `// largest Fibonacci numbers in an array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to create the hash table ` `// to check Fibonacci numbers ` `void` `createHash(set<` `int` `>& hash, ` ` ` `int` `maxElement) ` `{ ` ` ` `// Inserting the first two elements ` ` ` `// into the hash ` ` ` `int` `prev = 0, curr = 1; ` ` ` `hash.insert(prev); ` ` ` `hash.insert(curr); ` ` ` ` ` `// Computing the remaining ` ` ` `// elements using ` ` ` `// the previous two elements ` ` ` `while` `(curr <= maxElement) { ` ` ` `int` `temp = curr + prev; ` ` ` `hash.insert(temp); ` ` ` `prev = curr; ` ` ` `curr = temp; ` ` ` `} ` `} ` ` ` `// Function that calculates the sum ` `// and the product of K smallest and ` `// K largest Fibonacci numbers in an array ` `void` `fibSumAndProduct(` `int` `arr[], ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Find the maximum value in the array ` ` ` `int` `max_val = *max_element(arr, arr + n); ` ` ` ` ` `// Creating a hash containing ` ` ` `// all the Fibonacci numbers ` ` ` `// upto the maximum data value ` ` ` `// in the array ` ` ` `set<` `int` `> hash; ` ` ` `createHash(hash, max_val); ` ` ` ` ` `// Max Heap to store all the ` ` ` `// Fibonacci numbers ` ` ` `priority_queue<` `int` `> maxHeap; ` ` ` ` ` `// Min Heap to store all the ` ` ` `// Fibonacci numbers ` ` ` `priority_queue<` `int` `, ` ` ` `vector<` `int` `>, ` ` ` `greater<` `int` `> > ` ` ` `minHeap; ` ` ` ` ` `// Push all the fibonacci numbers ` ` ` `// from the array to the heaps ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(hash.find(arr[i]) ` ` ` `!= hash.end()) { ` ` ` ` ` `minHeap.push(arr[i]); ` ` ` `maxHeap.push(arr[i]); ` ` ` `} ` ` ` ` ` `long` `long` `int` `minProduct = 1, ` ` ` `maxProduct = 1, ` ` ` `minSum = 0, ` ` ` `maxSum = 0; ` ` ` ` ` `// Finding the K minimum ` ` ` `// and the K maximum ` ` ` `// elements from the heaps ` ` ` `while` `(k--) { ` ` ` ` ` `// Calculate the products ` ` ` `minProduct *= minHeap.top(); ` ` ` `maxProduct *= maxHeap.top(); ` ` ` ` ` `// Calculate the sum ` ` ` `minSum += minHeap.top(); ` ` ` `maxSum += maxHeap.top(); ` ` ` ` ` `// Pop the current ` ` ` `// minimum element ` ` ` `minHeap.pop(); ` ` ` ` ` `// Pop the current ` ` ` `// maximum element ` ` ` `maxHeap.pop(); ` ` ` `} ` ` ` ` ` `cout << ` `"Sum of K-minimum "` ` ` `<< ` `"fibonacci numbers is "` ` ` `<< minSum << ` `"\n"` `; ` ` ` `cout << ` `"Product of K-minimum "` ` ` `<< ` `"fibonacci numbers is "` ` ` `<< minProduct << ` `"\n"` `; ` ` ` `cout << ` `"Sum of K-maximum "` ` ` `<< ` `"fibonacci numbers is "` ` ` `<< maxSum << ` `"\n"` `; ` ` ` `cout << ` `"Product of K-maximum "` ` ` `<< ` `"fibonacci numbers is "` ` ` `<< maxProduct; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 2, 5, 6, 8, 10, 11 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `int` `K = 2; ` ` ` ` ` `fibSumAndProduct(arr, N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Sum of K-minimum fibonacci numbers is 7 Product of K-minimum fibonacci numbers is 10 Sum of K-maximum fibonacci numbers is 13 Product of K-maximum fibonacci numbers is 40

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.

## Recommended Posts:

- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Largest and smallest Fibonacci numbers in an Array
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Largest subset whose all elements are Fibonacci numbers
- Sum and Product of all Fibonacci Nodes of a Singly Linked List
- Check if a M-th fibonacci number divides N-th fibonacci number
- Maximum sum of smallest and second smallest in an array
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Pair of fibonacci numbers with a given sum and minimum absolute difference
- Find the smallest and second smallest elements in an array
- Difference between the largest and the smallest primes in an array
- Average of remaining elements after removing K largest and K smallest elements from array
- Maximize distance between smallest and largest Array elements by a single swap
- Sum of Fibonacci Numbers
- Even Fibonacci Numbers Sum
- Number of ways to represent a number as sum of k fibonacci numbers

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.