-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargest Fibonacci Subsequence
79 lines (65 loc) · 1.43 KB
/
Largest Fibonacci Subsequence
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Given an array with positive number the task to find the largest subsequence from array that contain elements which are Fibonacci numbers.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the array. Then in the next line are N space separated values of the array.
Output:
For each test case in a new line print the space separated elements of the longest fibonacci subsequence.
Constraints:
1<=T<=100
1<=N<=100
1<=A[]<=1000
Example:
Input:
2
7
1 4 3 9 10 13 7
9
0 2 8 5 2 1 4 13 23
Output:
1 3 13
0 2 8 5 2 1 13
CODE:
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static int fibo(int n)
{
int a=0,b=1,c=0,f=0;
while(c<=n)
{
if(c==n)
{
f=1;
}
a=b;
b=c;
c=a+b;
}
return f;
}
public static void main (String[] args) {
//code
Scanner in=new Scanner(System.in);
int t=in.nextInt();
int flag;
while(t--!=0)
{
int n=in.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=in.nextInt();
}
for(int i=0;i<n;i++)
{
flag=fibo(arr[i]);
if(flag==1)
{
System.out.print(arr[i]+" ");
}
}
System.out.println("");
}
}
}