Nth term of Ruler Function Series - GeeksforGeeks

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:
Append the smallest positive integer not present in the series.
Then, append all the numbers before the last number.
Examples:
Input: N = 5Output: 1Explanation: 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 = 8Output: 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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to count the number// of set bits in the number Nint 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 Seriesvoid findNthTerm(int N){    // Store the result    int x = setBits(N ^ (N - 1));     // Print the result    cout << x;} // Driver Codeint main(){    int N = 8;    findNthTerm(N);     return 0;}
// Java program for the above approachimport java.io.*;import java.lang.*;import java.util.*; class GFG{ // Function to count the number// of set bits in the number Nstatic 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 Seriesstatic void findNthTerm(int N){         // Store the result    int x = setBits(N ^ (N - 1));     // Print the result    System.out.println(x);} // Driver Codepublic static void main(String[] args){    int N = 8;         findNthTerm(N);}} // This code is contributed by Kingash
# Python3 program for the above approach # Function to count the number# of set bits in the number Ndef 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 Seriesdef findNthTerm(N):         # Store the result    x = setBits(N ^ (N - 1))     # Print the result    print(x) # Driver CodeN = 8 findNthTerm(N) # This code is contributed by Ankita saini
// C# program for the above approachusing 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.
<script> // Javascript program for the above approach // Function to count the number// of set bits in the number Nfunction setBits(n){         // Store the number of setbits    var 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 Seriesfunction findNthTerm(N){         // Store the result    var x = setBits(N ^ (N - 1));     // Print the result    document.write(x);} // Driver codevar N = 8;     findNthTerm(N); // This code is contributed by Khushboogoyal499 </script>
4
 
Time Complexity: O(log N)Auxiliary Space: O(1)
Kingash
khushboogoyal499
ankita_saini
ukasp
anikakapoor
Bitwise-XOR
series
setBitCount
Bit Magic
Mathematical
Pattern Searching
Mathematical
Bit Magic
series
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Reverse actual bits of the given number
Program to find parity
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7