## xyz Interview Question

**Country:**India

```
# Given a list of n numbers representing the first n elements of
# the sequence the kth-element of the sequence (k >= n+1) is
# given by the sum of the previous n elements in the sequence
# the method get_seq(n, generators) returns the first n elements
# of the sequence generated by the elements of the list generators
def get_seq(n, generators ) :
if n == 0 or len(generators) < 1:
print('Invalid input')
return
the_sequence = []
step = len(generators)
for i in range(0,len(generators)) :
the_sequence.append( generators[i])
print('step 0 el=%s' %the_sequence)
step = len(generators)
if step > n:
return generators[0:n]
while(step < n) :
next_element = 0
for i in range(0, len(generators)):
next_element += the_sequence[-1-i]
the_sequence.append(next_element)
step += 1
return the_sequence
#test
print(get_seq(14,[0,0,1,1]))
```

```
public static int lucas(int n)
{
int res[] = new int[n + 1];
for(int i = 0; i < res.length; i++){res[i] = -1;}
return lucas(n, res);
}
public static int lucas(int n, int res[])
{
if(res[n] != -1)
{
return res[n];
}else{
if(n == 0 || n == 1)
{
res[n] = 0;
return res[n];
}
if(n == 2)
{
res[n] = 1;
return res[n];
}
res[n] = lucas(n-1, res) + lucas(n-2, res) + lucas(n-3, res);
return res[n];
}
}
public static void main(String args[]){
for(int i = 0; i <= 10; i++)
{
System.out.println(lucas(i));
//0,0,1,1,2,4,7,13,24,44,81
}
}
```

```
package com.pubmatic.util;
public class Lucas {
public static void main(String[] args) {
Lucas lucas = new Lucas();
lucas.lucas(10);
}
private void lucas(int num) {
int thirdLast = 0;
int secondLast = 0;
int last = 1;
int current = 0;
System.out.print(thirdLast + "," + secondLast + "," + last + ",");
for (int i = 3; i < num; i++) {
current = last + secondLast + thirdLast;
System.out.print(current + ",");
int tmp = last;
last = current;
thirdLast = secondLast;
secondLast = tmp;
}
}
}
```

```
package com.pubmatic.util;
public class Lucas {
public static void main(String[] args) {
Lucas lucas = new Lucas();
lucas.lucas(10);
}
private void lucas(int num) {
int thirdLast = 0;
int secondLast = 0;
int last = 1;
int current = 0;
System.out.print(thirdLast + "," + secondLast + "," + last + ",");
for (int i = 3; i < num; i++) {
current = last + secondLast + thirdLast;
System.out.print(current + ",");
int tmp = last;
last = current;
thirdLast = secondLast;
secondLast = tmp;
}
}
}
```

- NoOne October 06, 2016