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 = 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 <bits/stdc++.h> 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
<script> Â
// Javascript program for the above approach Â
// Function to count the number // of set bits in the number N function 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 Series function findNthTerm(N) {          // Store the result     var x = setBits(N ^ (N - 1)); Â
    // Print the result     document.write(x); } Â
// Driver code var N = 8; Â Â Â Â Â findNthTerm(N); Â
// This code is contributed by Khushboogoyal499 Â
</script> |
4
Â
Time Complexity: O(log N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!