Open In App

Find the missing N-th term of the series 2, 2, 4, 4, 6, 8, 8 ….

Last Updated : 16 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, find the N-th term in the series 2, 2, 4, 4, 6, 8, 8………

Examples:

Input: N = 1
Output: 2

Input: N = 2
Output: 2

Input: N = 5
Output: 6

 

Approach: Upon careful observation of the given series, it can be seen that it consists of two different series combed together at alternate positions.

Original Series: 2, 2, 4, 4, 6, 8, 8, …
—————————————————————–
Considering only even places: 
=> 2, 2, 4, 4, 6, 8, 8, … 
=> 2, _, 4, _, 6, _, 8, … (considering 0-based indexing) 
=> 2, 4, 6, 8, … (an AP series)
As it can be seen, the numbers at even places forms an Arithmetic Progression in itself.
—————————————————————–
Similarly, Considering only odd places: 
=> 2, 2, 4, 4, 6, 8, 8, … 
=> _, 2, _, 4, _, 8, … (considering 0-based indexing) 
=> 2, 4, 8, … (a GP series)
As it can be seen, the numbers at odd places forms a Geometric Progression in itself.
 

Therefore, it can be said that the given series is a mixture of 2 different series where all the terms of A.P are situated at even places and all the terms of G.P are situated at odd places. 
So, on the basis of the above observation, follow the steps below to find the Nth missing term of the given series:

  1. If n is odd:
    1. The required term is the (n+1)/2th term of the A.P [2, 4, 6, 8 …].
    2. The nth term of the A.P can be calculated as a + (n – 1)d.
  2. If n is even :
    1. The required term is the n/2th term of the G.P [2, 4, 8 …].
    2. The nth term of the G.P can be calculated as a*rn-1

Below is the implementation of the above approach:

C++




// C++ code to find the n-th term
// of series 2, 2, 4, 4, 6, 8, 8...
#include <iostream>
#include <math.h>
using namespace std;
 
// Driver code
int main()
{
    int n = 15;
    int term;
 
    // If n is even
    if (n % 2 == 0) {
        term = n / 2;
 
        // a(first term) = 2
        // r(common ratio) = 2
        cout << 2 * pow(2, term - 1);
    }
 
    // If n is odd
    else {
        term = (n + 1) / 2;
 
        // a(first term) = 2
        // d(common ratio) = 2
        cout << 2 + (term - 1) * 2;
    }
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG {
  public static void main(String[] args)
  {
    int n = 15;
    int term;
 
    // If n is even
    if (n % 2 == 0) {
      term = n / 2;
 
      // a(first term) = 2
      // r(common ratio) = 2
      System.out.println(2 * Math.pow(2, term - 1));
    }
 
    // If n is odd
    else {
      term = (n + 1) / 2;
 
      // a(first term) = 2
      // d(common ratio) = 2
      System.out.println(2 + (term - 1) * 2);
    }
  }
}
 
// This code is contributed by Potta Lokesh


Python




# Python code to find the n-th term
# of series 2, 2, 4, 4, 6, 8, 8...
 
# Driver code
n = 15;
term = 0;
 
# If n is even
if (n % 2 == 0):
    term = n / 2;
 
    # a(first term) = 2
    # r(common ratio) = 2
    print(2 * int(pow(2, term - 1)));
 
# If n is odd
else:
    term = (n + 1) / 2;
 
    # a(first term) = 2
    # d(common ratio) = 2
    print(2 + int((term - 1) * 2));
     
# This code is contributed by Samim Hossain Mondal.


C#




// C# code for the above approach
using System;
 
class GFG {
    public static void Main(string[] args)
    {
        int n = 15;
        int term;
 
        // If n is even
        if (n % 2 == 0) {
            term = n / 2;
 
            // a(first term) = 2
            // r(common ratio) = 2
            Console.WriteLine(2 * Math.Pow(2, term - 1));
        }
 
        // If n is odd
        else {
            term = (n + 1) / 2;
 
            // a(first term) = 2
            // d(common ratio) = 2
            Console.WriteLine(2 + (term - 1) * 2);
        }
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
// Javascript code to find the n-th term
// of series 2, 2, 4, 4, 6, 8, 8...
 
// Driver code
let n = 15;
let term;
 
// If n is even
if (n % 2 == 0) {
    term = n / 2;
 
    // a(first term) = 2
    // r(common ratio) = 2
    document.write(2 * Math.pow(2, term - 1));
}
 
// If n is odd
else {
    term = (n + 1) / 2;
 
    // a(first term) = 2
    // d(common ratio) = 2
    document.write(2 + (term - 1) * 2);
}
 
// This code is contributed by gfgking.
</script>


Output: 

16

 

Time Complexity: O(logn) because using inbuilt pow function 
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads