Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ….
Last Updated :
19 Sep, 2022
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5…. The sequence is built in the following way: at first, the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4, and so on. Find the number on the n-th position of the sequence.
Examples:
Input : n = 3
Output : 2
The 3rd number in the sequence is 2.
Input : 55
Output : 10
First Approach: The idea is to first find the block number of n. To determine the block with the n-th number, we first subtract 1 (count of elements in the first block) from n, then subtract 2, then subtract 3 and so on until we got negative n. The number of subtractions will be the number of the block and the position in the block will be the last non-negative number we will get.
C++
#include <bits/stdc++.h>
using namespace std;
int findNumber( int n)
{
n--;
int i = 1;
while (n >= 0)
{
n -= i;
++i;
}
return (n + i);
}
int main()
{
int n = 3;
cout << findNumber(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findNumber( int n)
{
n--;
int i = 1 ;
while (n >= 0 )
{
n -= i;
++i;
}
return (n + i);
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(findNumber(n));
}
}
|
Python3
def findNumber( n ):
n - = 1
i = 1
while n > = 0 :
n - = i
i + = 1
return (n + i)
n = 3
print (findNumber(n))
|
C#
using System;
class GFG
{
static int findNumber( int n)
{
n--;
int i = 1;
while (n >= 0)
{
n -= i;
++i;
}
return (n + i);
}
public static void Main()
{
int n = 3;
Console.WriteLine(findNumber(n));
}
}
|
PHP
<?php
function findNumber( $n )
{
$n --;
$i = 1;
while ( $n >= 0)
{
$n -= $i ;
++ $i ;
}
return ( $n + $i );
}
$n = 3;
echo findNumber( $n );
?>
|
Javascript
<script>
function findNumber(n)
{
n--;
let i = 1;
while (n >= 0)
{
n -= i;
++i;
}
return (n + i);
}
let n = 3;
document.write(findNumber(n));
</script>
|
Time Complexity: O(?n)
Auxiliary space: -O(1)
Second Approach: The answer to the Infinite Sequence may be done in O(1). We may organize the sequence as: 1 (1). That means the positions that the first one in that line is 1, 2 (2) 1, 2, 3 (4) 1, 2, 3, 4 (7) 1, 2, 3, 4, 5 (11) Then we would have a new sequence, which is much easier to find. 1 (1) 2 (2) 4 (3) 7 (4) 11 The number in the parenthesis are the distance between the number of the new sequence.
To find the base of the number we need to solve the equation n = x(x+1)/2 + 1 [OR x^2 + x + 2 – 2n = 0]. We need to isolate x (get the max floor value).
Then we use the x we got in the same formula, but now the result is going to be the base for the line. We only need to calculate the distance between the number we receive as input and the number we got as base.
n — base + 1
C++
#include <bits/stdc++.h>
using namespace std;
int findNumber( int n)
{
int x = ( int ) floor ((-1 +
sqrt (1 + 8 * n - 8)) / 2);
int base = (x * (x + 1)) / 2 + 1;
return n - base + 1;
}
int main()
{
int n = 55;
cout << findNumber(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findNumber( int n)
{
int x = ( int )Math.floor((- 1 +
Math.sqrt( 1 + 8 * n - 8 )) / 2 );
int base = (x * (x + 1 )) / 2 + 1 ;
return n - base + 1 ;
}
public static void main(String[] args)
{
int n = 55 ;
System.out.println(findNumber(n));
}
}
|
Python3
import math
def findNumber( n ):
x = int (math.floor(( - 1 + math.sqrt( 1
+ 8 * n - 8 )) / 2 ))
base = (x * (x + 1 )) / 2 + 1
return n - base + 1
n = 55
print (findNumber(n))
|
C#
using System;
class GFG
{
static int findNumber( int n)
{
int x = ( int )Math.Floor((-1 +
Math.Sqrt(1 + 8 * n - 8)) / 2);
int Base = (x * (x + 1)) / 2 + 1;
return n - Base + 1;
}
public static void Main()
{
int n = 55;
Console.WriteLine(findNumber(n));
}
}
|
PHP
Javascript
Time Complexity: O(logn) because inbuilt sqrt function is being used
Auxiliary Space:O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...