BackmediumRecursionPaytmMicrosoft

Interleaved Recursive Sequences Solution

Problem Statement

Given two recursive sequences, a fuel sequence where each term is the sum of the previous two terms (starting with 23 and 17), and a resource sequence which alternates between the sum and difference of the previous two terms (starting with 11 and 7), implement a recursive function to find the nth term of the combined sequence where the fuel sequence and resource sequence are interleaved.

Example 1
Input
n = 1
Output
23

Explanation: For n = 1, the first term of the combined sequence is the first term of the fuel sequence, which is 23. This is because the fuel sequence and resource sequence are interleaved, and the first term of the combined sequence is the first term of the fuel sequence.

Example 2
Input
n = 3
Output
11

Explanation: For n = 3, the third term of the combined sequence is the first term of the resource sequence, which is 11. This is because the fuel sequence and resource sequence are interleaved, and the third term of the combined sequence is the first term of the resource sequence.

Constraints

  • 1 <= n <= 20
  • All terms are integers
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Interleaved Recursive Sequences — Problem Statement & Solution Guide

RecursionMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given two recursive sequences, a fuel sequence where each term is the sum of the previous two terms (starting with 23 and 17), and a resource sequence which alternates between the sum and difference of the previous two terms (starting with 11 and 7), implement a recursive function to find the nth term of the combined sequence where the fuel sequence and resource sequence are interleaved.

Examples

Example 1

Input

n = 1

Output

23

Explanation: For n = 1, the first term of the combined sequence is the first term of the fuel sequence, which is 23. This is because the fuel sequence and resource sequence are interleaved, and the first term of the combined sequence is the first term of the fuel sequence.

Example 2

Input

n = 3

Output

11

Explanation: For n = 3, the third term of the combined sequence is the first term of the resource sequence, which is 11. This is because the fuel sequence and resource sequence are interleaved, and the third term of the combined sequence is the first term of the resource sequence.

Constraints

  • 1 <= n <= 20
  • All terms are integers

Optimal Approach & Strategy

An optimized approach involves using memoization to store previously calculated terms, reducing the time complexity to O(n) by avoiding redundant calculations. This approach also uses a single recursive function to interleave the fuel and resource sequences.

Brute Force Approach

A brute-force approach would involve recursively calculating each term without storing previously calculated values, resulting in an inefficient solution with a time complexity of O(2^n). This approach would be impractical for larger inputs due to its exponential time complexity.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function interleaveSequences(fuel, resource, n) {
  if (n === 1) return fuel[0];
  let fuelIndex = Math.floor((n-1)/2);
  let resourceIndex = Math.floor((n-1)/2);
  if (n % 2 === 1) {
    return fuel[fuelIndex];
  } else {
    return resource[resourceIndex];
  }
}

Asked in Top Tech Interviews

PaytmMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.