Related Articles

Related Articles

Find Nth term of the series where each term differs by 6 and 2 alternately
  • Last Updated : 11 May, 2020

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find Nth term
void findNthTerm(int N)
{
    int ans = 0;
  
    // Iterate from 1 till Nth term
    for (int i = 0; i < N; i++) {
  
        // Check if i is even and
        // then add 6
        if (i % 2 == 0) {
            ans = ans + 6;
        }
  
        // Else add 2
        else {
            ans = ans + 2;
        }
    }
  
    // Print ans
    cout << ans << endl;
}
  
// Driver Code
int main()
{
    int N = 3;
    findNthTerm(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG{
   
// Function to find Nth term
static void findNthTerm(int N)
{
    int ans = 0;
   
    // Iterate from 1 till Nth term
    for (int i = 0; i < N; i++) {
   
        // Check if i is even and
        // then add 6
        if (i % 2 == 0) {
            ans = ans + 6;
        }
   
        // Else add 2
        else {
            ans = ans + 2;
        }
    }
   
    // Print ans
    System.out.print(ans +"\n");
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    findNthTerm(N);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to find Nth term
def findNthTerm(N):
    ans = 0
  
    # Iterate from 1 till Nth term
    for i in range(N):
  
        # Check if i is even and
        # then add 6
        if (i % 2 == 0) :
            ans = ans + 6
          
  
        # Else add 2
        else :
            ans = ans + 2
          
    # Print ans
    print(ans)
  
  
# Driver Code
if __name__=='__main__'
  
    N = 3
    findNthTerm(N)
      
  
# This code is contributed by AbhiThakur

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
// Function to find Nth term
static void findNthTerm(int N)
{
    int ans = 0;
  
    // Iterate from 1 till Nth term
    for (int i = 0; i < N; i++) {
  
        // Check if i is even and
        // then add 6
        if (i % 2 == 0) {
            ans = ans + 6;
        }
  
        // Else add 2
        else {
            ans = ans + 2;
        }
    }
  
    // Print ans
    Console.Write(ans +"\n");
}
  
// Driver Code
public static void Main()
{
    int N = 3;
    findNthTerm(N);
}
}
  
// This code is contributed by AbhiThakur

chevron_right


Output:

14

Time Complexity: O(N)

Efficient Approach: We can find the Nth term by using the below formula:

  1. If N is Odd: The Nth term is given by (N/2 + 1)*6 + (N/2)*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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find Nth term
void findNthTerm(int N)
{
    int ans;
  
    // Check if N is even
    if (N % 2 == 0) {
  
        // Formula for n is even
        ans = (N / 2) * 6
              + (N / 2) * 2;
    }
  
    // Check if N is odd
    else {
        // Formula for N is odd
        ans = (N / 2 + 1) * 6
              + (N / 2) * 2;
    }
  
    // Print ans
    cout << ans << endl;
}
  
// Driver Code
int main()
{
    int N = 3;
    findNthTerm(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
  
  
class GFG{
   
// Function to find Nth term
static void findNthTerm(int N)
{
    int ans;
   
    // Check if N is even
    if (N % 2 == 0) {
   
        // Formula for n is even
        ans = (N / 2) * 6
              + (N / 2) * 2;
    }
   
    // Check if N is odd
    else {
        // Formula for N is odd
        ans = (N / 2 + 1) * 6
              + (N / 2) * 2;
    }
   
    // Print ans
    System.out.print(ans +"\n");
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    findNthTerm(N);
}
}
  
// This code contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to find Nth term
def findNthTerm(N):
    ans = 0;
  
    # Check if N is even
    if (N % 2 == 0):
  
        # Formula for n is even
        ans = (N // 2) * 6 + (N // 2) * 2;
      
    # Check if N is odd
    else:
          
        # Formula for N is odd
        ans = (N // 2 + 1) * 6 + (N // 2) * 2;
      
    # Print ans
    print(ans);
      
# Driver Code
if __name__ == '__main__':
      
    N = 3;
    findNthTerm(N);
      
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
    
// Function to find Nth term
static void findNthTerm(int N)
{
    int ans;
    
    // Check if N is even
    if (N % 2 == 0) {
    
        // Formula for n is even
        ans = (N / 2) * 6
              + (N / 2) * 2;
    }
    
    // Check if N is odd
    else {
        // Formula for N is odd
        ans = (N / 2 + 1) * 6
              + (N / 2) * 2;
    }
    
    // Print ans
    Console.Write(ans +"\n");
}
    
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
    findNthTerm(N);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

14

Time Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :