[Question]
  [
Say you list the positive integers in a triangle, then flip it left-to-right. Given a number, output the number it's sent to. This is a self-inverse mapping.
```
         1                      1         
       2   3                  3   2       
     4   5   6    <--->     6   5   4     
   7   8   9  10         10   9   8   7   
11  12  13  14  15     15  14  13  12  11
```
This is the n'th element of [A038722](https://oeis.org/A038722), one-indexed:
```
1, 3, 2, 6, 5, 4, 10, 9, 8, 7, 15, 14, 13, 12, 11, ...
```
This sequence reverses contiguous chunks of the positive integers with increasing lengths:
```
 1, 3, 2, 6, 5, 4, 10, 9, 8, 7, 15, 14, 13, 12, 11, ...
<-><----><-------><-----------><------------------>
```
Test cases:
```
1 -> 1
2 -> 3
3 -> 2
4 -> 6
14 -> 12
990 -> 947
991 -> 1035
1000 -> 1026
1035 -> 991
1036 -> 1081
12345 -> 12305
```
---
**Leaderboard:**
```
var QUESTION_ID=117879,OVERRIDE_USER=20260;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/117879/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
```
```
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
```
```
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>
```
  
[Answer]
## JavaScript (ES7), 26 bytes
```
n=>((2*n)**.5+.5|0)**2-n+1
```
An implementation of the following formula [from OEIS](https://oeis.org/A038722):
[![formula](https://i.stack.imgur.com/FIbAc.png)](https://i.stack.imgur.com/FIbAc.png)
### Demo
```
let f =
n=>((2*n)**.5+.5|0)**2-n+1
for(n = 1; n <= 50; n++) {
  console.log(n, '->', f(n))
}
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~8~~ 7 bytes
```
RṁR€UFi
```
*Thanks to @ErikTheOutgolfer for saving 1 byte!*
[Try it online!](https://tio.run/nexus/jelly#@x/0cGdj0KOmNaFumf@P7jncDmS6//9vqKNgpKNgrKNgoqNgCMSWlgYgAihsaGBgACKNTcGkGQA "Jelly – TIO Nexus")
### How it works
```
RṁR€UFi  Main link. Argument: n
R        Range; yield [1, ..., n].
  R€     Range each; yield [[1], [1, 2], [1, 2, 3], ..., [1, ..., n]].
 ṁ       Mold the left argument like the right one, yielding
         [[1], [2, 3], [4, 5, 6], ...]. The elements of the left argument are 
         repeated cyclically to fill all n(n+1)/2 positions in the right argument.
    U    Upend; reverse each flat array, yielding [[1], [3, 2], [6, 5, 4], ...].
     F   Flatten, yielding [1, 3, 2, 6, 5, 4, ...].
      i  Index; find the first index of n in the result.
```
[Answer]
## [Alice](https://github.com/m-ender/alice), 27 bytes
*Thanks to Sp3000 for the `.C` idea.*
```
/o
\i@/.2:e2,tE*Y~Z.H2*~.C+
```
[Try it online!](https://tio.run/nexus/alice#@6@fzxWT6aCvZ2SVaqRT4qoVWRel52GkVafnrP3/v6GRsYkpAA "Alice – TIO Nexus")
### Explanation
I think there may be a shorter way to compute this using triangular numbers, but I thought this is an interesting abuse of a built-in, so here's a different solution.
The basic idea is to make use of Alice's "pack" and "unpack" built-ins. "Pack", or `Z`, takes two integers maps them bijectively to a single integer. "Unpack", or `Y`, inverts this bijection and turns one integer into two. Normally, this can be used to store a list or tree of integers in a single (large) integer and recover the individual values later. However, in this case we can use the functions in the opposite order, to let the nature of the bijection work for us.
Unpacking one integer into two integers basically consists of three steps:
1. Map **ℤ → ℕ** (including zero) with a simple "folding". That is, map negative integers to odd naturals, and non-negative integers to even naturals.
2. Map **ℕ → ℕ2**, using the [Cantor pairing function](https://en.wikipedia.org/wiki/Pairing_function#Cantor_pairing_function). That is, the naturals are written along the diagonals of an infinite grid and we return the indices:
```
   ...
3  9 ...
2  5 8 ...
1  2 4 7 ...
0  0 1 3 6 ...
   0 1 2 3
```
E.g. `8` would be mapped to the pair `(1, 2)`.
3. Map **ℕ2 → ℤ2**, using the inverse of step 1 on each integer individually. That is, odd naturals get mapped to negative integers, and even naturals get mapped to non-negative integers.
To pack two integers into one, we simply invert each of those steps.
Now, we can see that the structure of the Cantor pairing function conveniently encodes the triangle we need (although the values are off-by-one). To reverse those diagonals, all we need to do is swap the **x** and **y** coordinates into the grid.
Unfortunately, since all three of the above steps are combined into a single built-in `Y` (or `Z`), we need to undo the **ℤ → ℕ** or **ℕ → ℤ** mappings ourselves. However, while doing so we can save a couple of bytes by directly using **ℕ+ → ℤ** or **ℤ → ℕ+** mappings, to take care of the off-by-one error in the table. So here is the entire algorithm:
1. Map **ℕ+ → ℤ** using **(n/2) \* (-1)n-1**. This mapping is chosen such that it cancels the implicit **ℤ → ℕ** mapping during unpacking, except that it shifts the value down by 1.
2. Unpack the result into two integers.
3. Swap them.
4. Pack the swapped values into a single integer again.
5. Map **ℤ → ℕ+** using **|2n| + (n≥0)**. Again, this mapping is chosen such that it cancels the implicit **ℕ → ℤ** mapping during packing, except that it shifts the value up by 1.
With that out of the way, we can look at the program:
```
/o
\i@/...
```
This is simply a framework for linear arithmetic programs with integer input and output.
```
.    Duplicate the input.
2:   Halve it.
e    Push -1.
2,   Pull up the other copy of the input.
t    Decrement.
E    Raise -1 to this power.
*    Multiply. We've now computed (n/2) * (-1)^(n-1).
Y    Unpack.
~    Swap.
Z    Pack.
.H   Duplicate the result and take its absolute value.
2*   Double.
~    Swap with other copy.
.C   Compute k-choose-k. That's 1 for k ≥ 0 and 0 for k < 0.
+    Add. We've now computed |2n| + (n≥0).
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
Ḥ½+.Ḟ²‘_
```
[Try it online!](https://tio.run/nexus/jelly#ARkA5v//4bikwr0rLuG4nsKy4oCYX////zEyMzQ1 "Jelly – TIO Nexus")
Port of my MATL answer.
[Answer]
# [Haskell](https://www.haskell.org/), 31 bytes
```
r=round
f n=r(sqrt$2*n)^2-r n+1
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/v8i2KL80L4UrTSHPtkijuLCoRMVIK08zzki3SCFP2/B/bmJmnm1BUWZeiUqagvF/AA "Haskell – Try It Online")
This answer just uses the formula. It is the least interesting answer here, but it also happens to be the golfiest.
# [Haskell](https://www.haskell.org/), ~~38~~ ~~36~~ 34 bytes
```
x!y|x<=y=1-x|v<-y+1=v+(x-y)!v
(!0)
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/v0KxsqbCxrbS1lC3oqbMRrdS29C2TFujQrdSU7GMK9FWQ9FA839uYmaebUFRZl6JSm5igUJitKGenqGBQex/AA "Haskell – Try It Online")
`(!0)` is the point free function we are concerned with.
## Explanation
Let me start out by saying I'm very happy with this answer.
The basic idea here is that if we remove the largest triangular number smaller than our input we can reverse it and add the triangular number back. So we define an operator `!`, `!` takes our regular input, `x`, but it also takes an extra number `y`. `y` keeps track of the size of the growing triangular number. If `x>y` we want to recurse, we decrease `x` by `y` and increase `y` by one. So we calculate `(x-y)!(y+1)` and add `y+1` to it. If `x<=y` we have reached our base case, to reverse `x`'s placement in the row of the triangle we return `1-x`.
# [Haskell](https://www.haskell.org/), 54 bytes
```
f x|u<-div(x^2-x)2=[u+x,u+x-1..u+1]
(!!)$0:(>>=)[1..]f
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P02hoqbURjcls0yjIs5It0LTyDa6VLtCB4h1DfX0SrUNY7kSbTUUFTVVDKw07OxsNaOBwrFp/3MTM/NsC4oy80pUchMLFBJBwoYGBrH/AQ "Haskell – Try It Online")
`(!!)$0:(>>=)[1..]f` is a point-free function
## Explanation
The first thing we are concerned with is `f`, `f` is a function that takes `x` and returns the `x`th row of th triangle in reverse. It does this by first calculating the `x-1`nd triangular number and assigning it to `u`. `u<-div(x^2-x)2`. We then return the list `[u+x,u+x-1..u+1]`. `u+x` is the `x`th triangular number and the first number on the row, `u+x-1` is one less than that and the second number on the row `u+1` is one more than the last triangular number and thus the last number on the row.
Once we have `f` we form a list `(>>=)[1..]f`, which is a flattening of the triangle. We add zero to the front with `0:` so that our answers will not be offset by one, and supply it to our indexing function `(!!)`.
# [Haskell](https://www.haskell.org/), 56 bytes
```
f 0=[0]
f x|u<-f(x-1)!!0=[u+x,u+x-1..u+1]
(!!)$[0..]>>=f
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P03BwDbaIJYrTaGiptRGN02jQtdQU1ERKFiqXaEDxLqGenql2oaxXIm2GoqKmirRBnp6sXZ2tmn/cxMz82wLijLzSlRyEwsUEqOBKg0NDGL/AwA "Haskell – Try It Online")
This one is 2 bytes longer but a bit more elegant in my opinion.
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~15~~ 11 bytes
```
EX^.5+kUG-Q
```
[Try it online!](https://tio.run/nexus/matl#@@8aEadnqp0d6q4b@P@/oZGxiSkA "MATL – TIO Nexus")
This uses the formula
```
a(n) = floor(sqrt(2*n)+1/2)^2 - n + 1.
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/), ~~71~~ 68 bytes
*3 bytes saved thanks to [Conor O'Brien](https://codegolf.stackexchange.com/users/31957/conor-obrien).*
```
x=triu(ones(n=input('')));x(~~x)=1:nnz(x);disp(nonzeros(flip(x))(n))
```
This doesn't work for large inputs due to memory limitations.
[Try it online!](https://tio.run/nexus/octave#Dcg7CsAgEAXA4/hel5RGvE0MLIRV/MBi4dVNppxtsVcZyJoaNIqW0eEcyWBYyxjPS3XCGG5pBZp1ppobnlfKv4SSe3t/fA "Octave – TIO Nexus")
### Explanation
Consider input `n = 4`. The code first builds the matrix
```
 1     1     1     1
 0     1     1     1
 0     0     1     1
 0     0     0     1
```
Then it replaces nonzero entries in column-major order (down, then across) by `1`, `2`, `3` ... :
```
 1     2     4     7
 0     3     5     8
 0     0     6     9
 0     0     0    10
```
Then it flips the matrix vertically:
```
 0     0     0    10
 0     0     6     9
 0     3     5     8
 1     2     4     7
```
Finally, it takes the `n`-th nonzero value in column-major order, which in this case is `6`.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 48 bytes
```
k,j,l;f(n){for(k=j=0;k<n;)l=k,k+=++j;n=1+k-n+l;}
```
[Try it online!](https://tio.run/nexus/c-gcc#LY3RCoMwDEXf/YogCC2N0KgbSOx@ZPNhDxNqXTecb@K3u9TtQs4NSbjZA4448aCiXofXrIIbneXQRdaTCxiMM2bk6MiEMpqJt/1591HpNQORjwssj89yJerBwQqEUCHUCA0CSbWtTZAxWWsT69PBs7CqmxNsfCSl3ynNS4xlsQ6IxI3Rxz7pPcvFoPLCQ3mBwt9ijr/3vkcY1L/VmrNt/wI "C (gcc) – TIO Nexus")
Probably suboptimal, but I'm pretty happy with this one. Uses the fact that
NTF**N** = T**N** + [A057944](http://oeis.org/A057944)(**N**) - **N** + 1
(If I wrote the formula down correctly, that is.)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 30 bytes
```
U1V[YLO>X›iYLOX-UY<LO>X+,q}Y>V
```
[Try it online!](https://tio.run/nexus/05ab1e#@x9qGBYd6eNvF/GoYVcmkBGhGxppA@Jr6xTWRtqF/f8PAA)
[Answer]
## Batch, 70 bytes
```
@set/ai=%2+1,j=%3+i
@if %j% lss %1 %0 %1 %i% %j%
@cmd/cset/ai*i+1-%1
```
Uses a loop to find the index of the triangular number at least as large as `n`.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 6 bytes
```
!ṁ↔´CN
```
[Try it online!](https://tio.run/##yygtzv7/X/HhzsZHbVMObXH2@///v6GBgQEA "Husk – Try It Online")
### Explanation
```
!ṁ↔´CN  -- implicit input N, for example: 4
   ´ N  -- duplicate the natural numbers:
           [1,2,3,…] [1,2,3,…]
    C   -- cut the second argument into sizes of the first:
           [[1],[2,3],[4,5,6],[7,8,9,10],…]
 ṁ↔     -- map reverse and flatten:
           [1,3,2,6,5,4,10,9,8,7,15,…
!       -- index into that list:
           6
```
[Answer]
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/tinylisp), 78 bytes
```
(d _(q((R N T)(i(l T N)(_(a R 1)N(a T R))(a 2(a T(s T(a N R
(d f(q((N)(_ 2 N 1
```
Defines a function `f` that performs the mapping. [Try it online!](https://tio.run/##FYs7CsAgEET7nGLK2dJArmEh9rIhBAQTzafJ6c1aDG8Y3rz5/Ep@Wu/ckHiRAR5RmFkQ4YWJigAn3hgRRIzz6HwsanaY7LuP79Ax2@Q6S9UNJa@33p9MPLRhB91b4RaR/gM "tinylisp – Try It Online")
### Ungolfed
We find the smallest triangular number that is greater than or equal to the input number, as well as which row of the triangle our number is in. From these, we can calculate the flipped version of the number.
* If the current triangular number is less than N, recurse to the next row of the triangle. (We treat the top row as row 2 to make the math simpler.)
* Otherwise, the flipped version of N is (T-N)+(T-R)+2.
The main function `flip` simply calls the helper function `_flip` starting from the top row.
```
(load library)
(def _flip
 (lambda (Num Row Triangular)
  (if (less? Triangular Num)
   (_flip Num (inc Row) (+ Triangular Row))
   (+ 2
    (- Triangular Num)
    (- Triangular Row))))))
(def flip
 (lambda (Num) (_flip Num 2 1)))
```
[Answer]
# [Julia 1.0](http://julialang.org/), ~~29~~ 23 bytes
```
!n=round((2n)^.5)^2-n+1
```
[Try it online!](https://tio.run/##yyrNyUw0rPj/XzHPtii/NC9FQ8MoTzNOz1Qzzkg3T9vwf0FRZl5JTp6GooahpiYXEs8ElWtkbGKqqfkfAA "Julia 1.0 – Try It Online")
Port of [Arnauld's JS](https://codegolf.stackexchange.com/a/117883/109916) answer.
-6 thx to @steffan
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
·LD£í˜¹<è
```
[Try it online!](https://tio.run/nexus/05ab1e#ARUA6v//wrdMRMKjw63LnMK5PMOo//80MDA "05AB1E – TIO Nexus")
**Explanation**
```
·L          # push range [1 ... 2n]
  D         # duplicate
   £        # split the first list into pieces with size dependent on the second list
    í       # reverse each sublist
     ˜      # flatten
      ¹<è   # get the element at index <input>-1
```
Array flattening unfortunately doesn't handle larger lists very well.  
At the cost of 1 byte we could do [·t2z+ïn¹->](https://tio.run/nexus/05ab1e#@39oe4lRlfbh9XmHdura/f9vaWkIAA) using the mathematical formula `floor(sqrt(2*n)+1/2)^2 - n + 1` found on [OEIS](https://oeis.org/A038722).
[Answer]
# [Pyt](https://github.com/mudkip201/pyt), ~~13~~ 12 bytes
```
←Đ2*√½+⌊²-~⁺
```
Port of [Arnauld's approach](https://codegolf.stackexchange.com/a/117883/15214)
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 8 bytes
```
dɾ:ẇRfJi
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCJkyb464bqHUmZKaSIsIiIsIjQwMCJd)
[Answer]
# [Python 3](https://docs.python.org/3/), ~~37~~ ~~35~~ 32 bytes
```
lambda n:round((2*n)**.5)**2-n+1
```
[Try it online!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUSHPqii/NC9FQ8NIK09TS0vPFEgY6eZpG/4vKMrMK9FI0zDU1OSCs02QOUbGJqaamv8B "Python 3 – Try It Online")
Port of [Arnauld's JS](https://codegolf.stackexchange.com/a/117883/109916) answer.
-3 thx to @Steffan
[Answer]
# PHP, 35 Bytes
```
<?=((2*$argn)**.5+.5^0)**2-$argn+1;
```
Same formula that is used in [Arnaulds Approach](https://codegolf.stackexchange.com/questions/117879/number-triangle-flip/117883#117883)
[Answer]
# C#, ~~46~~ 44 bytes
```
n=>Math.Pow((int)(Math.Sqrt(2*n)+.5),2)-n+1;
```
I port [@Arnauld's solution](https://codegolf.stackexchange.com/a/117883/15214). Thank you!
* Pow of 0.5 is Sqrt. 2 bytes saved!
[Answer]
# APL (Dyalog), 27 bytes
I've got two solutions at the same bytecount.
A train:
```
⊢⊃⊃∘(,/{⌽(+/⍳⍵-1)+⍳⍵}¨∘⍳)
```
[Try it online!](https://tio.run/nexus/apl-dyalog#@@/4qG3Co65Fj7qaQahjhoaOfvWjnr0a2vqPejc/6t2qa6ipDWHVHloBlAeyNf//dwSyezcbGgAA "APL (Dyalog Unicode) – TIO Nexus")
And a dfn:
```
{⍵⊃⊃((⍳⍵),.{1+⍵-⍳⍺}+\⍳⍵)}
```
[Try it online!](https://tio.run/nexus/apl-dyalog#@@/4qG1C9aPerY@6moFIQ@NR72YgT1NHr9pQG8jQBfN31WrHQCVq//93PLQCyDE0AAA "APL (Dyalog Unicode) – TIO Nexus")
Both of these solutions first create the flipped triangle and then extract element at the index stated by the argument (`1`-based).
[Answer]
# J, 25 bytes
```
3 :'>:y-~*:>.-:<:%:>:8*y'
```
As an explanation, consider `f(n) = n(n+1)/2`. `f(r)`, given the row `r`, returns the leftmost number of the the `r`th row of the mirrored triangle. Now, consider `g(n) = ceiling[f⁻¹(n)]`. `g(i)`, given the index `i`, returns the row on which index i is found. Then, `f(g(n))` returns the leftmost number of the row on which index n is found. So, `h(n) = f(g(n)) - (n - f(g(n)-1)) + 1` is the answer to the above problem. 
Simplifying, we get `h(n) = [g(n)]² - n + 1 = ceiling[(-1 + sqrt(1 + 8n))/2]² - n + 1`.
From the looks of @Arnauld's formula, it appears that: 
`ceiling[(-1 + sqrt(1 + 8n))/2] = floor[1/2 + sqrt(2n)]`.
[Answer]
# [J](http://jsoftware.com/), 19 bytes
```
1-]-0.5*:@<.@+%:@+:
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuo2CgYAXEunoKzkE@bv8NdWN1DfRMtawcbPQctFWtHLSt/mtypSZn5CukKRjCGEYwhjGMYQJXA2dZWhogmHCthgYGBgi2sSkS2wzONjI2Mf0PAA "J – Try It Online") Uses the formula from [Arnauld's answer](https://codegolf.stackexchange.com/a/117883/31957).
```
1-]-0.5*:@<.@+%:@+:
                 +:    2n
              %:@      sqrt(^)
    0.5      +         0.5 + ^
          <.@          floor(^)
       *:@             square(^)
  ]-                   input - ^
1-                     1 - ^
                       (computes - input + 1 in a golfier way)
```
# [J](http://jsoftware.com/), 43 bytes
```
1+;@((#{.[:;(1<@,#&0)"0)<@|.;.1>:)@i.@+:i.]
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuo2CgYAXEunoKzkE@bv8Nta0dNDSUq/Wiraw1DG0cdJTVDDSVDDRtHGr0rPUM7aw0HTL1HLStMvVi/2typSZn5CukKRjCGEYwhjGMYQJXA2dZWhogmHCthgYGBgi2sSkS2wzONjI2Mf0PAA "J – Try It Online")
Shortest non-formula solution I could think of. The array form of `;.` is really unwieldly for code golf, and it doesn't help that ragged lists aren't that golfy either.
```
1+;@((#{.[:;(1<@,#&0)"0)<@|.;.1>:)@i.@+:i.]
                                  @i.@+:      range [0, 2n)
    (                       ;.1>:)            segment [1, 2n] according to
     (                 )                      the indices of triangular numbers:
            (       )"0                         over each integer in [0, 2n):
                 #&0                            create that many 0s
             1  ,                               prepend a 1 (a cut location)
              <@                                and box it
         [:;                                    then raze that boxed list
      #{.                                       and take 2n entries from that array
                        <@|.                  box and reverse each segment
  ;@                                          raze this list
                                        i.]   get the 0-based index of the input in it
1+                                            and add 1
```
For generating the indices of triangular number, the invariance under inverse transform `(=(-:@*>:)@<.@%:@+:)` is 1 byte longer, but much faster.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 11 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
0-indexed
```
gUòò@T±1ÃcÔ
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Z1Xy8kBUsTHDY9Q&input=MTE)
```
gUòò@T±1ÃcÔ     :Implicit input of integer U
g               :Index into
 Uò             :  Range [0,U]
   ò            :  Range [0,each]
    @           :  Map each subrange
     T±1        :    Increment T (initially 0) by 1
        Ã       :  End map
         c      :  Flat map
          Ô     :    Reverse
```
]