给定一个整数K和一个包含N 个整数的数组 arr[] ,任务是找到数组中 K 个最小和K 个最大斐波那契数的和和乘积。
注意:假设数组中至少有K 个斐波那契数。
例子:
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
方法:这个想法是使用散列来预先计算和存储斐波那契节点直到最大值,在一个集合中,使检查变得容易和高效(在 O(1) 时间内)。
- 遍历整个数组,获取列表中的最大值。
- 现在,构建一个哈希表,其中包含小于或等于数组最大值的所有斐波那契节点。
执行上述预计算后,遍历数组并将所有斐波那契数插入两个堆,一个最小堆和一个最大堆。
现在,从最小堆和最大堆中弹出顶部 K 个元素来计算 K 个斐波那契数的和和乘积。
下面是上述方法的实现:
// C++ program to find the sum and
// product of K smallest and K
// largest Fibonacci numbers in an array
#include
using namespace std;
// Function to create the hash table
// to check Fibonacci numbers
void createHash(set& 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 hash;
createHash(hash, max_val);
// Max Heap to store all the
// Fibonacci numbers
priority_queue maxHeap;
// Min Heap to store all the
// Fibonacci numbers
priority_queue,
greater >
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;
}
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
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。