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

04 Jan, 2022
Given a number N, find the N-th term in the series 2, 2, 4, 4, 6, 8, 8.........
Examples:
Input: N = 1Output: 2
Input: N = 2Output: 2
Input: N = 5Output: 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:
If n is odd:The required term is the (n+1)/2th term of the A.P [2, 4, 6, 8 ...].The nth term of the A.P can be calculated as a + (n – 1)d.If n is even :The required term is the n/2th term of the G.P [2, 4, 8 ...].The nth term of the G.P can be calculated as a*rn-1
If n is odd:The required term is the (n+1)/2th term of the A.P [2, 4, 6, 8 ...].The nth term of the A.P can be calculated as a + (n – 1)d.
The required term is the (n+1)/2th term of the A.P [2, 4, 6, 8 ...].The nth term of the A.P can be calculated as a + (n – 1)d.
The required term is the (n+1)/2th term of the A.P [2, 4, 6, 8 ...].
The nth term of the A.P can be calculated as a + (n – 1)d.
If n is even :The required term is the n/2th term of the G.P [2, 4, 8 ...].The nth term of the G.P can be calculated as a*rn-1
The required term is the n/2th term of the G.P [2, 4, 8 ...].The nth term of the G.P can be calculated as a*rn-1
The required term is the n/2th term of the G.P [2, 4, 8 ...].
The nth term of the G.P can be calculated as a*rn-1
Below is the implementation of the above approach:
C++
Java
Python
C#
Javascript
// 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 codeint 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 code for the above approachimport 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 code to find the n-th term# of series 2, 2, 4, 4, 6, 8, 8... # Driver coden = 15;term = 0; # If n is evenif (n % 2 == 0):    term = n / 2;     # a(first term) = 2    # r(common ratio) = 2    print(2 * int(pow(2, term - 1))); # If n is oddelse:    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# code for the above approachusing 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.
<script>// Javascript code to find the n-th term// of series 2, 2, 4, 4, 6, 8, 8... // Driver codelet n = 15;let term; // If n is evenif (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 oddelse {    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>
16
 
Time Complexity: O(1) Auxiliary Space: O(1)
lokeshpotta20
gfgking
ukasp
samim2000
nnr223442
arithmetic progression
Geometric Progression
series
Mathematical
Pattern Searching
Mathematical
series
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Modular multiplicative inverse
Count ways to reach the n'th stair
Program to multiply two matrices
KMP Algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Naive algorithm for Pattern Searching
Check if a string is substring of another
Boyer Moore Algorithm for Pattern Searching