- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum and product of K smallest and largest Fibonacci numbers in the array
In this problem, we will find the sum and product of the K maximum and minimum Fibonacci numbers in the array.
The given problem is very basic and aims to focus on improving the problem-solving skills of beginners. The main goal of the problem is to introduce how to filter the Fibonacci numbers from the given array of elements and sum and product minimum and maximum Fibonacci numbers.
Problem Statement
We have given a nums[] array containing the N integer values. Also, we have given K positive integer. We need to find the sum and product of the K minimum and maximum Fibonacci elements of the array.
Note − It is given that array always contains minimum K Fibonacci numbers.
Sample Examples
Input
nums[] = {2, 1, 9, 55, 13, 68}; K = 3;
Output
Minimum sum = 16 Minimum product = 26 Maximum sum = 70 Maximum product = 1430
Explanation
The Fibonacci numbers in the given array are [2, 1, 55, 13]. We have taken minimum and maximum 3 elements from these 4 elements to find the sum and products.
Input
nums[] = {3, 13, 21, 55, 34, 89, 144, 233}; K = 2;
Output
Minimum sum = 16 Minimum product = 39 Maximum sum = 377 Maximum product = 33552
Explanation
All elements of the given array are Fibonacci numbers. So, the minimum 2 elements are [3, 13], and the maximum 4 elements are [233, 144]
Approach
In this approach, we will find the maximum element of the array. After that, we will find all Fibonacci numbers in the range of 1 to the maximum element.
Next, we will use the two priority queues to store all Fibonacci numbers. One priority queue will use the max head to store the numbers in the decreasing orders, and another will use the min heap to store the numbers in the increasing order. After that, we can take the first K elements from the queue and add or multiply them.
Algorithm
Step 1 − Get the maximum element from the array elements.
Step 2 − Define the fibSet named set and pass it as a getFibs() function parameter to get all Fibonacci numbers in the range of 1 to max element.
Step 2.1 − In the getFibs() function, initialize the left and right variables with 0 and 1, respectively. Also, insert them in the fibSet set.
Step 2.3 − Make iterations while the value of the 'right' variable is less than the 'maxEle' value.
Step 2.4 − Store the left + right in the temp. Also, insert the 'temp' value in the set.
Step 2.5 − Update the left with the right and the right with the temp value.
Step 3 − Now, create a min_fib and max_fib priority queues. The min_fib should use the min head, and max_fib should use the max head which is by default.
Step 4 − Now, insert all elements of the fibSet set into the min_fib and max_fib queue.
Step 5 − Initialize the min_p and max_p with 1 to store the product of K minimum and maximum Fibonacci elements. Also, initialize the min_s and max_s with 0 to store the sum of K minimum and maximum Fibonacci elements.
Step 6 − Extract the first K elements from both queues, add them, and multiply them.
Step 7 − Print resultant values.
Example
#include <bits/stdc++.h> using namespace std; void getFibs(set<int> &fibSet, int maxEle) { // 0 and 1 is Fibonacci number int left = 0, right = 1; fibSet.insert(left); fibSet.insert(right); // Find Fibbonacci numbers while (right <= maxEle) { int temp = right + left; fibSet.insert(temp); left = right; right = temp; } } void findSumAndProduct(int nums[], int n, int k) { // Get the maximum element of the array int maxEle = *max_element(nums, nums + n); // Get all Fibonacci numbers in the range of 1 to maxEle set<int> fibSet; getFibs(fibSet, maxEle); // Max and min heap to store fibonacci numbers priority_queue<int> max_fib; priority_queue<int, vector<int>, greater<int>> min_fib; // Insert fibonacci numbers into heaps for (int p = 0; p < n; p++) { if (fibSet.find(nums[p]) != fibSet.end()) { min_fib.push(nums[p]); max_fib.push(nums[p]); } } long long int min_p = 1, max_p = 1, min_s = 0, max_s = 0; // Get first K elements from min and max heap while (k--) { // For products min_p *= min_fib.top(); max_p *= max_fib.top(); // For sum min_s += min_fib.top(); max_s += max_fib.top(); // Rremove front elements from queue min_fib.pop(); max_fib.pop(); } cout << "The sum of K minimum fibonacci elements is " << min_s << "\n"; cout << "The product of K minimum fibonacci elements is " << min_p << "\n"; cout << "The sum of K maximum fibonacci elements is " << max_s << "\n"; cout << "The product of K maximum fibonacci elements is " << max_p; } int main() { int nums[] = {2, 1, 9, 55, 13, 68}; int N = sizeof(nums) / sizeof(nums[0]); int K = 3; findSumAndProduct(nums, N, K); return 0; }
Output
The sum of K minimum fibonacci elements is 16 The product of K minimum fibonacci elements is 26 The sum of K maximum fibonacci elements is 70 The product of K maximum fibonacci elements is 1430
Time complexity − O(NLogN) to insert elements into the queue.
Space complexity − O(N) to store Fibonacci numbers in the set.
Conclusion
This problem has introduced 2 basic concepts. One finds the Fibonacci numbers in the given range, and another uses the priority queue. Whenever we are required to find the minimum or maximum K elements among N elements, we should use the priority queue. However, we can achieve the same result by sorting the array.