โ† TCS questions
CodingAverage

Find the n-th number in a mixed power series

Filed under TCS

Free read 1 of 3

Check if n is even or odd using the modulo operator (n % 2).

The question

Given an integer n, find the n-th term of a mixed series. In this series, all odd-indexed positions (1st, 3rd, 5th...) are powers of 3 (3, 9, 27, 81...), and all even-indexed positions (2nd, 4th, 6th...) are powers of 2 (2, 4, 8, 16...).

Constraints

  • 1 <= n <= 30
  • n is a positive integer

Examples

Input
n = 1
Output
3
Why
The 1st term is the first power of 3 (3^1).
Input
n = 2
Output
2
Why
The 2nd term is the first power of 2 (2^1).
Input
n = 5
Output
27
Why
The 5th term is the third power of 3 (3^3).

Approach

  1. Check if n is even or odd using the modulo operator (n % 2).

Solution

O(log n) time, O(1) space

Python
class Solution:
    def findNthTerm(self, n: int) -> int:
        # Determine if position is odd or even
        if n % 2 != 0:
            # Odd position: Power of 3
            exponent = (n + 1) // 2
            return 3 ** exponent
        else:
            # Even position: Power of 2
            exponent = n // 2
            return 2 ** exponent

Tips

  • What they are testing: Verify the logic for both small odd and even values of n to ensure the exponent calculation is correct.
  • Common mistake: Using 0-based indexing logic when the problem description implies 1-based indexing for 'odd' and 'even' positions.

Related TCS questions

Collected from public candidate posts and coding forums. Updated Nov 2026.