Wednesday, September 25, 2024
Google search engine
HomeData Modelling & AIFind Nth term of the series where each term differs by 6...

Find Nth term of the series where each term differs by 6 and 2 alternately

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




// 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;
}


Java




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


Python3




# 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


C#




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


Javascript




<script>
 
// JavaScript program for the above approach
   
    // Function to find Nth term
    function findNthTerm(N)
    {
        let ans = 0;
   
        // Iterate from 1 till Nth term
        for (let 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
        document.write(ans + "<br>");
    }
   
    // Driver Code
       let N = 3;
       findNthTerm(N);
  
 
// This code is contributed by Mayank Tyagi
 
</script>


Output: 

14

 

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: 

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




// 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;
}


Java




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


Python3




# 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


C#




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


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to find Nth term
function findNthTerm( N)
{
    let ans;
 
    // Check if N is even
    if (N % 2 == 0) {
 
        // Formula for n is even
        ans = parseInt(N / 2) * 6
              + parseInt(N / 2) * 2;
    }
 
    // Check if N is odd
    else {
        // Formula for N is odd
        ans = parseInt(N / 2 + 1) * 6
              + parseInt(N / 2) * 2;
    }
 
    // Print ans
     document.write(ans);
}
 
// Driver Function
 
    // get the value of N
    let N = 3;
 
    // Calculate and print the Nth term
    findNthTerm(N);
 
// This code is contributed by todaysgaurav
 
</script>


Output: 

14

 

Time Complexity: O(1)

Auxiliary Space: O(1) since using constant variables
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments