import
java.util.*;
public
class
FibonacciSumProduct {
public
static
void
createHash(HashSet<Integer> hash,
int
maxElement)
{
int
prev =
0
, curr =
1
;
hash.add(prev);
hash.add(curr);
while
(curr <= maxElement) {
int
temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
public
static
void
fibSumAndProduct(
int
[] arr,
int
n,
int
k)
{
int
max_val = Arrays.stream(arr).max().getAsInt();
HashSet<Integer> hash =
new
HashSet<>();
createHash(hash, max_val);
PriorityQueue<Integer> maxHeap
=
new
PriorityQueue<>(
Collections.reverseOrder());
PriorityQueue<Integer> minHeap
=
new
PriorityQueue<>();
for
(
int
i =
0
; i < n; i++)
if
(hash.contains(arr[i])) {
minHeap.add(arr[i]);
maxHeap.add(arr[i]);
}
long
minProduct = 1L, maxProduct = 1L, minSum = 0L,
maxSum = 0L;
while
(k-- >
0
) {
minProduct *= minHeap.peek();
maxProduct *= maxHeap.peek();
minSum += minHeap.peek();
maxSum += maxHeap.peek();
minHeap.poll();
maxHeap.poll();
}
System.out.println(
"Sum of K-minimum "
+
"fibonacci numbers is "
+ minSum);
System.out.println(
"Product of K-minimum "
+
"fibonacci numbers is "
+ minProduct);
System.out.println(
"Sum of K-maximum "
+
"fibonacci numbers is "
+ maxSum);
System.out.println(
"Product of K-maximum "
+
"fibonacci numbers is "
+ maxProduct);
}
public
static
void
main(String[] args)
{
int
[] arr = {
2
,
5
,
6
,
8
,
10
,
11
};
int
n = arr.length;
int
k =
2
;
fibSumAndProduct(arr, n, k);
}
}