Find Nth term of the series where each term differs by 6 and 2 alternately
Last Updated :
05 Aug, 2022
Given a number N, the task is to find the Nth term of the series where each term differs by 6 and 2 alternately.
Examples:
Input: N = 6
Output: 24
Explanation:
The Nth term is 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24
Input: N = 3
Output: 14
Explanation:
The Nth term is 0 + 6 + 2 + 6 = 14
Naive Approach: The idea is to iterate from 1 with an increment of 6 and 2 alternatively, till we reach the Nth term.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNthTerm( int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
ans = ans + 6;
}
else {
ans = ans + 2;
}
}
cout << ans << endl;
}
int main()
{
int N = 3;
findNthTerm(N);
return 0;
}
|
Java
class GFG{
static void findNthTerm( int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (i % 2 == 0 ) {
ans = ans + 6 ;
}
else {
ans = ans + 2 ;
}
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int N = 3 ;
findNthTerm(N);
}
}
|
Python3
def findNthTerm(N):
ans = 0
for i in range (N):
if (i % 2 = = 0 ) :
ans = ans + 6
else :
ans = ans + 2
print (ans)
if __name__ = = '__main__' :
N = 3
findNthTerm(N)
|
C#
using System;
class GFG{
static void findNthTerm( int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
ans = ans + 6;
}
else {
ans = ans + 2;
}
}
Console.Write(ans + "\n" );
}
public static void Main()
{
int N = 3;
findNthTerm(N);
}
}
|
Javascript
<script>
function findNthTerm(N)
{
let ans = 0;
for (let i = 0; i < N; i++) {
if (i % 2 == 0) {
ans = ans + 6;
}
else {
ans = ans + 2;
}
}
document.write(ans + "<br>" );
}
let N = 3;
findNthTerm(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) as using constant variables
Efficient Approach: We can find the Nth term by using the below formula:
- If N is Odd: The Nth term is given by (N/2 + 1)*6 + (N/2)*2.
- If N is Even: The Nth term is given by (N/2)*6 + (N/2)*2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNthTerm( int N)
{
int ans;
if (N % 2 == 0) {
ans = (N / 2) * 6
+ (N / 2) * 2;
}
else {
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
cout << ans << endl;
}
int main()
{
int N = 3;
findNthTerm(N);
return 0;
}
|
Java
class GFG{
static void findNthTerm( int N)
{
int ans;
if (N % 2 == 0 ) {
ans = (N / 2 ) * 6
+ (N / 2 ) * 2 ;
}
else {
ans = (N / 2 + 1 ) * 6
+ (N / 2 ) * 2 ;
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int N = 3 ;
findNthTerm(N);
}
}
|
Python3
def findNthTerm(N):
ans = 0 ;
if (N % 2 = = 0 ):
ans = (N / / 2 ) * 6 + (N / / 2 ) * 2 ;
else :
ans = (N / / 2 + 1 ) * 6 + (N / / 2 ) * 2 ;
print (ans);
if __name__ = = '__main__' :
N = 3 ;
findNthTerm(N);
|
C#
using System;
class GFG{
static void findNthTerm( int N)
{
int ans;
if (N % 2 == 0) {
ans = (N / 2) * 6
+ (N / 2) * 2;
}
else {
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int N = 3;
findNthTerm(N);
}
}
|
Javascript
<script>
function findNthTerm( N)
{
let ans;
if (N % 2 == 0) {
ans = parseInt(N / 2) * 6
+ parseInt(N / 2) * 2;
}
else {
ans = parseInt(N / 2 + 1) * 6
+ parseInt(N / 2) * 2;
}
document.write(ans);
}
let N = 3;
findNthTerm(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1) since using constant variables
Share your thoughts in the comments
Please Login to comment...