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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the 
// value at n-th place in 
// the given sequence
  
  
// Definition of findNumber function
function findNumber($n)
{
    // Finding x from equation 
    // n = x(x + 1)/2 + 1
    $x = floor((-1 + 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;
echo findNumber($n) ;
  
// This code is contributed by anuj_67.
?>

chevron_right



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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



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.