TCS NINJA MIXED SERIES 1

clock icon

Asked 8 months ago

message

0Comments

eye

1 Views

Given Problem Statement !

Consider the following series: 1,1,2,3,4,9,8,27,16,81,32,243,64,729,128,2187... This series is a mixture of 2 series – all the odd terms in this series form a geometric series and all the even terms form yet another geometric series. Write a program to find the Nth term in the series. The value N in a positive integer that should be read from STDIN. The Nth term that is calculated by the program should be written to STDOUT. Other than value of n th term,no other character / string or message should be written to STDOUT. For example , if N=16, the 16th term in the series is 2187, so only value 2187 should be printed.

#include <iostream>
#include <cmath> // For pow function

using namespace std;

int main() {
    int N;
    cin >> N;
    
    if (N % 2 == 1) {
        // Odd-indexed term
        int termIndex = (N / 2); // N/2 gives the 0-based index for odd terms
        cout << pow(2, termIndex) << endl;
    } else {
        // Even-indexed term
        int termIndex = (N / 2) - 1; // (N/2 - 1) gives the 0-based index for even terms
        cout << pow(3, termIndex) << endl;
    }
    
    return 0;
}

N is the position of the term we're looking for in the combined series. For example:

  • If N = 1, we want the 1st term
  • If N = 5, we want the 5th term
  • If N = 16, we want the 16th term

Why we're using those index mappings:

  1. For odd N (N / 2 as exponent for 2):
    • Odd positions in the combined series correspond to the terms in the power-of-2 series.
    • We divide N by 2 because each term in the power-of-2 series appears every other position.
    • Example:
      • For N = 5 (5th term), (5 / 2) = 2, so we calculate 2^2 = 4
      • Indeed, the 5th term in the combined series is 4
  2. For even N ((N / 2) - 1 as exponent for 3):
    • Even positions in the combined series correspond to the terms in the power-of-3 series.
    • We divide N by 2 because each term in the power-of-3 series appears every other position.
    • We subtract 1 because the first even term (N = 2) should use 3^0, not 3^1.
    • Example:
      • For N = 6 (6th term), (6 / 2) - 1 = 2, so we calculate 3^2 = 9
      • Indeed, the 6th term in the combined series is 9

Test Case: Let's say N = 10

  1. First, we identify which series this belongs to: 10 is an even number, so it's part of the power-of-3 series.
  2. We apply the formula for even N: (N / 2) - 1 = (10 / 2) - 1 = 5 - 1 = 4
  3. Now we calculate 3^4: 3^4 = 81
  4. Let's verify this by looking at the full series up to the 10th term: 1, 1, 2, 3, 4, 9, 8, 27, 16, 81

Indeed, the 10th term (N = 10) is 81, which matches our calculation.

Let's break down why this works:

  • The 2nd term (3^0 = 1) is at position 2
  • The 4th term (3^1 = 3) is at position 4
  • The 6th term (3^2 = 9) is at position 6
  • The 8th term (3^3 = 27) is at position 8
  • The 10th term (3^4 = 81) is at position 10

You can see that the exponent is always (N / 2) - 1 for the power-of-3 series.

Similarly, for the power-of-2 series (odd positions):

  • The 1st term (2^0 = 1) is at position 1
  • The 3rd term (2^1 = 2) is at position 3
  • The 5th term (2^2 = 4) is at position 5
  • The 7th term (2^3 = 8) is at position 7
  • The 9th term (2^4 = 16) is at position 9

Here, the exponent is always N / 2 (integer division) for the power-of-2 series.

Let's break this down more clearly:

  1. For even N (2, 4, 6, 8, 10, 12, ...):
    • These correspond to the series: 1, 3, 9, 27, 81, 243, ... (powers of 3)
  2. For odd N (1, 3, 5, 7, 9, 11, ...):
    • These correspond to the series: 1, 2, 4, 8, 16, 32, ... (powers of 2)

You're absolutely right to point this out. The even indices in the combined series give us the odd-looking series (powers of 3), while the odd indices give us the even-looking series (powers of 2).

This is why in the code:

  • For odd N, we use 2 as the base (pow(2, termIndex))
  • For even N, we use 3 as the base (pow(3, termIndex))

 

Share

Write your comment here !

0 Responses