Sum and product of K smallest and largest Fibonacci numbers in the array

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).

  1. Traverse through the entire array and obtain the maximum value in the list.
  2. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.