BackmediumRecursionInfosys

Galactic Supply Chain Optimizer Solution

Problem Statement

Given an integer n (1-indexed), compute the nth term of two recursively defined sequences. The fuel sequence F starts with F1=23, F2=17 and for i≥3 follows F_i = F_{i-1}+F_{i-2}. The resource sequence R starts with R1=11, R2=7 and for i≥3 alternates between sum and difference: if i is odd then R_i = R_{i-1}+R_{i-2}, otherwise R_i = R_{i-1}-R_{i-2}. Input consists of a single integer n. Output two integers F_n and R_n separated by a single space.

Example 1
Input
1
Output
23 11

Explanation: F1=23, R1=11.

Example 2
Input
3
Output
40 18

Explanation: F3=F2+F1=17+23=40. R3=R2+R1=7+11=18.

Example 3
Input
4
Output
57 11

Explanation: F4=F3+F2=40+17=57. R4=R3-R2=18-7=11.

Example 4
Input
5
Output
97 29

Explanation: F5=F4+F3=57+40=97. R5=R4+R3=11+18=29.

Example 5
Input
6
Output
154 18

Explanation: F6=F5+F4=97+57=154. R6=R5-R4=29-11=18.

Constraints

  • 1 <= n <= 100000
  • Values of F_n and R_n fit within 64-bit signed integer
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

Galactic Supply Chain Optimizer — Problem Statement & Solution Guide

RecursionMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given an integer n (1-indexed), compute the nth term of two recursively defined sequences. The fuel sequence F starts with F1=23, F2=17 and for i≥3 follows F_i = F_{i-1}+F_{i-2}. The resource sequence R starts with R1=11, R2=7 and for i≥3 alternates between sum and difference: if i is odd then R_i = R_{i-1}+R_{i-2}, otherwise R_i = R_{i-1}-R_{i-2}. Input consists of a single integer n. Output two integers F_n and R_n separated by a single space.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Galactic Supply Chain Optimizer"

medium

WHY DOES IT MATTER?

Linear recurrences appear in many algorithmic problems—Fibonacci, Tribonacci, and dynamic programming state transitions. Recognizing the pattern allows you to replace exponential recursion with linear or logarithmic solutions, drastically improving performance.

OPTIMIZATION CHALLENGE

The key insight is that each term depends only on a fixed number of predecessors, so you can discard older values and keep a sliding window of size equal to the recurrence order.

REAL-WORLD CONNECTION

Consider a supply chain where each day's inventory depends on the previous two days. Efficiently forecasting inventory levels is analogous to computing terms of a linear recurrence; using DP prevents recomputing past forecasts.

When explaining to an interviewer, emphasize that you’re exploiting the recurrence’s linearity to achieve O(n) time and O(1) space, and mention that matrix exponentiation is a common optimization for very large n.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem defines two linear recurrence sequences: a classic Fibonacci-like sequence for fuel F and an alternating sum/difference sequence for resources R. Naïve recursive evaluation of F_i or R_i would recompute the same subproblems exponentially many times, leading to O(2^n) time and stack overflow for large n. The optimal paradigm is to use iterative dynamic programming or closed‑form matrix exponentiation. By storing only the last two computed values, we can compute each term in constant time, achieving O(n) time and O(1) space. For very large n, matrix exponentiation (fast doubling) reduces the time to O(log n), but for the constraints typical in interview settings, the linear DP suffices and is easier to implement correctly.

Interview Questions on This Problem

Q1How would you modify the algorithm if the sequences were defined with a different recurrence, such as F_i = 2*F_{i-1} + 3*F_{i-2} and R_i = R_{i-1} * R_{i-2} for odd i and R_i = R_{i-1} / R_{i-2} for even i?

You would still use iterative DP, but you must handle multiplication/division and potential overflow. For division, ensure integer division semantics or use floating point. The key is to maintain only the last two values and update them according to the new coefficients.

Q2In a distributed system, how could you parallelize the computation of the nth term of a Fibonacci-like sequence?

You can use matrix exponentiation with exponentiation by squaring, which can be parallelized by computing powers of the transition matrix concurrently. Alternatively, you can split the range into blocks, compute partial results, and combine them using the associative property of matrix multiplication.

Q3What would be the impact on time complexity if you were required to output all terms up to n instead of just the nth term?

Outputting all terms requires O(n) time regardless of the method, as you must compute each term once. The space complexity would increase to O(n) if you store all terms, but you can still compute them in O(1) space if you only need the last two values for each step.

Examples

Example 1

Input

1

Output

23 11

Explanation: F1=23, R1=11.

Example 2

Input

3

Output

40 18

Explanation: F3=F2+F1=17+23=40. R3=R2+R1=7+11=18.

Example 3

Input

4

Output

57 11

Explanation: F4=F3+F2=40+17=57. R4=R3-R2=18-7=11.

Example 4

Input

5

Output

97 29

Explanation: F5=F4+F3=57+40=97. R5=R4+R3=11+18=29.

Example 5

Input

6

Output

154 18

Explanation: F6=F5+F4=97+57=154. R6=R5-R4=29-11=18.

Constraints

  • 1 <= n <= 100000
  • Values of F_n and R_n fit within 64-bit signed integer

Optimal Approach & Strategy

Iteratively compute each term in a single loop, updating two variables that hold the last two values; this runs in linear time and constant space.

Brute Force Approach

A naïve approach would recursively compute each term, calling the function twice for every non‑base case, leading to exponential time and stack overflow for large n.

Verified Code Solutions

JavaScript Solution
Time: O(n)
/**
 * @param {number} n
 * @return {number[]}
 */
var computeSequences = function(n) {
    if (n === 1) return [23, 11];
    if (n === 2) return [17, 7];
    
    let f1 = 23, f2 = 17;
    let r1 = 11, r2 = 7;
    
    for (let i = 3; i <= n; i++) {
        let f3 = f1 + f2;
        f1 = f2;
        f2 = f3;
        
        let r3;
        if (i % 2 === 1) {
            r3 = r1 + r2;
        } else {
            r3 = r1 - r2;
        }
        r1 = r2;
        r2 = r3;
    }
    
    return [f2, r2];
};

Asked in Top Tech Interviews

Infosys

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.