1.

The Golomb sequence is a non-decreasing integer sequence in which the n-th term equals the number of times the letter n appears in the sequence. The first few numbers are 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5,......

Answer»

Definitions of a few terms:
The third term is 2, and it's worth noting that three appears twice.
The SECOND term is 2, and it's worth noting that two appears twice.
The FOURTH term is 3, and it's worth noting that the number three appears three times.

If n is a POSITIVE integer, SOLVE for it. The goal is to discover the first n Golomb sequence terms.

To determine the nth term of a Golomb sequence, use the following recurrence relation:

  • a(1) = 1
  • a(n + 1) = 1 + a(n + 1 – a(a(n)))

The following is the Dynamic Programming implementation:

void findGolomb(int N){ int dp[N + 1]; // BASE case dp[1] = 1; // Calculating and displaying the first // N terms of Golomb Sequence. for (int i = 1; i <= N; i++) { dp[i] = 1 + dp[i - dp[dp[i - 1]]]; cout << dp[i] << " "; }}Useful Interview Resources
  • Free Mock Coding Interview
  • Coding Interview Questions for Practice
  • Technical Interview Questions for Practice
  • Interview Preparation


Discussion

No Comment Found