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.
// CPP program to find the // value at n-th place in // the given sequence #include <bits/stdc++.h> using namespace std;
// Returns n-th number in sequence // 1, 1, 2, 1, 2, 3, 1, 2, 4, ... int findNumber( int n)
{ n--;
// One by one subtract counts
// elements in different blocks
int i = 1;
while (n >= 0)
{
n -= i;
++i;
}
return (n + i);
} // Driver code int main()
{ int n = 3;
cout << findNumber(n) << endl;
return 0;
} |
// Java program to find the // value at n-th place in // the given sequence import java.io.*;
class GFG
{ // Returns n-th number in sequence
// 1, 1, 2, 1, 2, 3, 1, 2, 4, ...
static int findNumber( int n)
{
n--;
// One by one subtract counts
// elements in different blocks
int i = 1 ;
while (n >= 0 )
{
n -= i;
++i;
}
return (n + i);
}
// Driver Code
public static void main(String[] args)
{
int n = 3 ;
System.out.println(findNumber(n));
}
} // This code is contributed by Ajit. |
# Python code to find the value at # n-th place in the given sequence # Returns n-th number in sequence # 1, 1, 2, 1, 2, 3, 1, 2, 4, ... def findNumber( n ):
n - = 1
# One by one subtract counts
# elements in different blocks
i = 1
while n > = 0 :
n - = i
i + = 1
return (n + i)
# Driver code n = 3
print (findNumber(n))
# This code is contributed # by "Sharad_Bhardwaj". |
// C# program to find the // value at n-th place in // the given sequence using System;
class GFG
{ // Returns n-th number in sequence
// 1, 1, 2, 1, 2, 3, 1, 2, 4, ...
static int findNumber( int n)
{
n--;
// One by one subtract counts
// elements in different blocks
int i = 1;
while (n >= 0)
{
n -= i;
++i;
}
return (n + i);
}
// Driver code
public static void Main()
{
int n = 3;
Console.WriteLine(findNumber(n));
}
} // This code is contributed by vt_m. |
<?php // PHP program to find the // value at n-th place in // the given sequence // Returns n-th number in sequence // 1, 1, 2, 1, 2, 3, 1, 2, 4, ... function findNumber( $n )
{ $n --;
// One by one subtract counts
// elements in different blocks
$i = 1;
while ( $n >= 0)
{
$n -= $i ;
++ $i ;
}
return ( $n + $i );
} // Driver code $n = 3;
echo findNumber( $n );
// This code is contributed by anuj_67. ?> |
<script> // Javascript program to find the
// value at n-th place in
// the given sequence
// Returns n-th number in sequence
// 1, 1, 2, 1, 2, 3, 1, 2, 4, ...
function findNumber(n)
{
n--;
// One by one subtract counts
// elements in different blocks
let i = 1;
while (n >= 0)
{
n -= i;
++i;
}
return (n + i);
}
// Driver code let n = 3;
document.write(findNumber(n));
// This code is contributed by mukesh07.
</script> |
2
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
// CPP program to find the // value at n-th place in // the given sequence #include <bits/stdc++.h> using namespace std;
// Definition of findNumber // function int findNumber( int n)
{ // Finding x from equation
// n = x(x + 1)/2 + 1
int x = ( int ) floor ((-1 +
sqrt (1 + 8 * n - 8)) / 2);
// Base of current block
int base = (x * (x + 1)) / 2 + 1;
// Value of n-th element
return n - base + 1;
} // Driver code int main()
{ int n = 55;
cout << findNumber(n) << endl;
return 0;
} |
// Java program to find the // value at n-th place in // the given sequence import java.io.*;
class GFG
{ // Definition of findNumber function
static int findNumber( int n)
{
// Finding x from equation
// n = x(x + 1)/2 + 1
int x = ( int )Math.floor((- 1 +
Math.sqrt( 1 + 8 * n - 8 )) / 2 );
// Base of current block
int base = (x * (x + 1 )) / 2 + 1 ;
// Value of n-th element
return n - base + 1 ;
}
// Driver code
public static void main(String[] args)
{
int n = 55 ;
System.out.println(findNumber(n));
}
} // This code is contributed by Ajit. |
# Python program to find # the value at n-th place # in the given sequence import math
# Definition of findNumber function def findNumber( n ):
# Finding x from equation
# n = x(x + 1)/2 + 1
x = int (math.floor(( - 1 + math.sqrt( 1
+ 8 * n - 8 )) / 2 ))
# Base of current block
base = (x * (x + 1 )) / 2 + 1
# Value of n-th element
return n - base + 1
# Driver code n = 55
print (findNumber(n))
# This code is contributed # by "Abhishek Sharma 44" |
// C# program to find the // value at n-th place in // the given sequence using System;
class GFG
{ // Definition of findNumber function
static int findNumber( int n)
{
// Finding x from equation
// n = x(x + 1)/2 + 1
int x = ( int )Math.Floor((-1 +
Math.Sqrt(1 + 8 * n - 8)) / 2);
// Base of current block
int Base = (x * (x + 1)) / 2 + 1;
// Value of n-th element
return n - Base + 1;
}
// Driver code
public static void Main()
{
int n = 55;
Console.WriteLine(findNumber(n));
}
} // This code is contributed by vt_m. |
10
Time Complexity: O(logn) because inbuilt sqrt function is being used
Auxiliary Space:O(1)