# Nth term of Ruler Function Series

• Difficulty Level : Hard
• Last Updated : 25 Jun, 2021

Given a positive integer N, the task is to find the Nth term of the Ruler Function Series.

The Ruler Function Series is a series, having 1 as the first term, formed by performing the following two operations:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

• Append the smallest positive integer not present in the series.
• Then, append all the numbers before the last number.

Examples:

Input: N = 5
Output: 1
Explanation: The Ruler Function Series is given by {1, 2, 1, 3, 1, 2, 1, 4, ….. }. Therefore, the 5th term of the series is 1.

Input: N = 8
Output: 4

Naive Approach: The simplest approach to solve the given problem is to generate the series till the Nth term and then print the Nth term.

Time Complexity: O(N)
Auxiliary Space: O(N)

Efficient Approach: The above approach can also be optimized based on the following observation that the Nth element in the Ruler Function Series is equal to the number of set bits in (N^(N – 1)) as illustrated below:

• Number of set bits in the bitwise XOR of 0 and 1 = 1
• Number of set bits in the bitwise XOR of 1 and 2 = 2
• Number of set bits in the bitwise XOR of 2 and 3 = 1
• Number of set bits in the bitwise XOR of 3 and 4 = 3
• Number of set bits in the bitwise XOR of 4 and 5 = 1
• Number of set bits in the bitwise XOR of 5 and 6 = 2
• Number of set bits in the bitwise XOR of 6 and 7 = 1
• Number of set bits in the bitwise XOR of 7 and 8 = 4
• and so on…

Therefore, from the above observations, the Nth term of the Ruler Function Series is given by the Bitwise XOR of N and (N – 1)

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to count the number``// of set bits in the number N``int` `setBits(``long` `n)``{``    ``// Store the number of setbits``    ``int` `count = 0;` `    ``while` `(n > 0) {` `        ``// Update the value of n``        ``n = n & (n - 1);` `        ``// Update the count``        ``count++;``    ``}` `    ``// Return the total count``    ``return` `count;``}` `// Function to find the Nth term of``// the Ruler Function Series``void` `findNthTerm(``int` `N)``{``    ``// Store the result``    ``int` `x = setBits(N ^ (N - 1));` `    ``// Print the result``    ``cout << x;``}` `// Driver Code``int` `main()``{``    ``int` `N = 8;``    ``findNthTerm(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `class` `GFG{` `// Function to count the number``// of set bits in the number N``static` `int` `setBits(``long` `n)``{``    ` `    ``// Store the number of setbits``    ``int` `count = ``0``;` `    ``while` `(n > ``0``)``    ``{``        ` `        ``// Update the value of n``        ``n = n & (n - ``1``);` `        ``// Update the count``        ``count++;``    ``}` `    ``// Return the total count``    ``return` `count;``}` `// Function to find the Nth term of``// the Ruler Function Series``static` `void` `findNthTerm(``int` `N)``{``    ` `    ``// Store the result``    ``int` `x = setBits(N ^ (N - ``1``));` `    ``// Print the result``    ``System.out.println(x);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``8``;``    ` `    ``findNthTerm(N);``}``}` `// This code is contributed by Kingash`

## Python3

 `# Python3 program for the above approach` `# Function to count the number``# of set bits in the number N``def` `setBits(n):``    ` `    ``# Store the number of setbits``    ``count ``=` `0` `    ``while` `(n > ``0``):``        ` `        ``# Update the value of n``        ``n ``=` `n & (n ``-` `1``)` `        ``# Update the count``        ``count ``+``=` `1``        ` `    ``# Return the total count``    ``return` `count` `# Function to find the Nth term of``# the Ruler Function Series``def` `findNthTerm(N):``    ` `    ``# Store the result``    ``x ``=` `setBits(N ^ (N ``-` `1``))` `    ``# Print the result``    ``print``(x)` `# Driver Code``N ``=` `8` `findNthTerm(N)` `# This code is contributed by Ankita saini`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {` `    ``// Function to count the number``    ``// of set bits in the number N``    ``static` `int` `setBits(``long` `n)``    ``{` `        ``// Store the number of setbits``        ``int` `count = 0;` `        ``while` `(n > 0) {` `            ``// Update the value of n``            ``n = n & (n - 1);` `            ``// Update the count``            ``count++;``        ``}` `        ``// Return the total count``        ``return` `count;``    ``}` `    ``// Function to find the Nth term of``    ``// the Ruler Function Series``    ``static` `void` `findNthTerm(``int` `N)``    ``{` `        ``// Store the result``        ``int` `x = setBits(N ^ (N - 1));` `        ``// Print the result``        ``Console.WriteLine(x);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int` `N = 8;` `        ``findNthTerm(N);``    ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``
Output:
`4`

Time Complexity: O(log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up