# Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ….

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

First Approach: The idea is to first find block number of n. To determine the block with the n-th number, we first subtract 1 (count of elements in 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++

 `// CPP program to find the  ` `// value at n-th place in  ` `// the given sequence ` `#include ` `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

 `// 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. `

## Python3

 `# 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#

 `// 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

 `= 0)  ` `    ``{ ` `        ``\$n` `-= ``\$i``; ` `        ``++``\$i``; ` `    ``} ` ` `  `    ``return` `(``\$n` `+ ``\$i``); ` `} ` ` `  `// Driver code ` `\$n` `= 3; ` `echo` `findNumber(``\$n``); ` ` `  `// This code is contributed by anuj_67. ` `?> `

Output :

```2
```

Time Complexity: O(√n)

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++

 `// CPP program to find the  ` `// value at n-th place in  ` `// the given sequence ` `#include ` `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

 `// 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. `

## Python3

 `# 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#

 `// 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. `

## PHP

 ` `

Output :

`10 `

Time Complexity :
O(1)

This article is contributed by Sagar Shukla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.