-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFibionaciiList.java
47 lines (39 loc) · 1.07 KB
/
FibionaciiList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package collectionz;
//Java program to find
//largest Fibonacci subset
import java.util.*;
class FibionaciiList
{
// Prints largest subset of an array whose
// all elements are fibonacci numbers
public static void findFibSubset(Integer[] x)
{
Integer max = Collections.max(Arrays.asList(x));
List<Integer> fib = new ArrayList<Integer>();
List<Integer> result = new ArrayList<Integer>();
// Generate all Fibonacci numbers
// till max and store them
Integer a = 0;
Integer b = 1;
while (b < max){
Integer c = a + b;
a=b;
b=c;
fib.add(c);
}
// Now iterate through all numbers and
// quickly check for Fibonacci
for (Integer i = 0; i < x.length; i++){
if(fib.contains(x[i])){
result.add(x[i]);
}
}
System.out.println(result);
}
// Driver code
public static void main(String args[])
{
Integer[] a = {4, 2, 8, 5, 20, 1, 40, 13, 23};
findFibSubset(a);
}
}