Nth term of Ruler Function Series
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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int setBits( long n)
{
int count = 0;
while (n > 0) {
n = n & (n - 1);
count++;
}
return count;
}
void findNthTerm( int N)
{
int x = setBits(N ^ (N - 1));
cout << x;
}
int main()
{
int N = 8;
findNthTerm(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int setBits( long n)
{
int count = 0 ;
while (n > 0 )
{
n = n & (n - 1 );
count++;
}
return count;
}
static void findNthTerm( int N)
{
int x = setBits(N ^ (N - 1 ));
System.out.println(x);
}
public static void main(String[] args)
{
int N = 8 ;
findNthTerm(N);
}
}
|
Python3
def setBits(n):
count = 0
while (n > 0 ):
n = n & (n - 1 )
count + = 1
return count
def findNthTerm(N):
x = setBits(N ^ (N - 1 ))
print (x)
N = 8
findNthTerm(N)
|
C#
using System;
class GFG {
static int setBits( long n)
{
int count = 0;
while (n > 0) {
n = n & (n - 1);
count++;
}
return count;
}
static void findNthTerm( int N)
{
int x = setBits(N ^ (N - 1));
Console.WriteLine(x);
}
public static void Main( string [] args)
{
int N = 8;
findNthTerm(N);
}
}
|
Javascript
<script>
function setBits(n)
{
var count = 0;
while (n > 0)
{
n = n & (n - 1);
count++;
}
return count;
}
function findNthTerm(N)
{
var x = setBits(N ^ (N - 1));
document.write(x);
}
var N = 8;
findNthTerm(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...