Find the missing N-th term of the series 2, 2, 4, 4, 6, 8, 8 ….
Last Updated :
16 Aug, 2022
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:
- 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
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n = 15;
int term;
if (n % 2 == 0) {
term = n / 2;
cout << 2 * pow (2, term - 1);
}
else {
term = (n + 1) / 2;
cout << 2 + (term - 1) * 2;
}
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int n = 15 ;
int term;
if (n % 2 == 0 ) {
term = n / 2 ;
System.out.println( 2 * Math.pow( 2 , term - 1 ));
}
else {
term = (n + 1 ) / 2 ;
System.out.println( 2 + (term - 1 ) * 2 );
}
}
}
|
Python
n = 15 ;
term = 0 ;
if (n % 2 = = 0 ):
term = n / 2 ;
print ( 2 * int ( pow ( 2 , term - 1 )));
else :
term = (n + 1 ) / 2 ;
print ( 2 + int ((term - 1 ) * 2 ));
|
C#
using System;
class GFG {
public static void Main( string [] args)
{
int n = 15;
int term;
if (n % 2 == 0) {
term = n / 2;
Console.WriteLine(2 * Math.Pow(2, term - 1));
}
else {
term = (n + 1) / 2;
Console.WriteLine(2 + (term - 1) * 2);
}
}
}
|
Javascript
<script>
let n = 15;
let term;
if (n % 2 == 0) {
term = n / 2;
document.write(2 * Math.pow(2, term - 1));
}
else {
term = (n + 1) / 2;
document.write(2 + (term - 1) * 2);
}
</script>
|
Time Complexity: O(logn) because using inbuilt pow function
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...