[Question]
  [
Given N, output the Nth term of this infinite sequence:
```
-1 2 -2 1 -3 4 -4 3 -5 6 -6 5 -7 8 -8 7 -9 10 -10 9 -11 12 -12 11 ... etc.
```
N may be 0-indexed or 1-indexed as you desire.
For example, if 0-indexed then inputs `0`, `1`, `2`, `3`, `4` should produce respective outputs `-1`, `2`, `-2`, `1`, `-3`.
If 1-indexed then inputs `1`, `2`, `3`, `4`, `5` should produce respective outputs `-1`, `2`, `-2`, `1`, `-3`.
To be clear, this sequence is generated by taking the sequence of positive integers repeated twice
```
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 ...
```
and rearranging each pair of odd numbers to surround the even numbers just above it
```
1 2 2 1 3 4 4 3 5 6 6 5 7 8 8 7 9 10 10 9 11 12 12 11 ...
```
and finally negating every other term, starting with the first
```
-1 2 -2 1 -3 4 -4 3 -5 6 -6 5 -7 8 -8 7 -9 10 -10 9 -11 12 -12 11 ...
```
**The shortest code in bytes wins.**
  
[Answer]
# [Python 2](https://docs.python.org/2/), 23 bytes
```
lambda n:~n/2+n%2*(n|2)
```
Odd inputs give roughly `n/2`, even ones roughly `-n/2`. So, I started with `-n/2+n%2*n` and tweaked from there. 
[Try it online!](https://tio.run/nexus/python2#BcFLCoAgFAXQca3iTkTtQ/BqJNRKmhiWBHULaRht3c6J45wPfy7Bg@5jJzWVVIav2LxdCcROJM@4GhmsK4s77XyglQS0E1QfNBQMG0RDa/MP "Python 2 – TIO Nexus")
[Answer]
# Mathematica, 29 bytes
```
((#~GCD~4/. 4->-2)+#)/2(-1)^#&
```
Pure function taking a 1-indexed input. Other than the alternating signs `(-1)^#`, twice the sequence is close to the input, the differences being 1, 2, 1, -2 cyclically. It's nice that `#~GCD~4`, the greatest common divisor of the input and `4`, is 1, 2, 1, 4 cyclically; so we just manually replace `4->-2` and call it a day. I like this approach because it avoids most of the many-character Mathematica commands.
[Answer]
# [Pip](https://github.com/dloscutoff/pip), ~~24~~ 22 bytes
```
v**a*YaBA2|1+:--a//4*2
```
Takes input, 1-indexed, as a command-line argument. [Try it online](https://tio.run/nexus/pip#@1@mpZWoFZno5GhUY6htpaubqK9vomX0//9/UwA "Pip – TIO Nexus") or [verify 1-20](https://tio.run/nexus/pip#q/5fpqWVqBWZ6ORoVGOobaWrm6ivb6Jl9L/WN0bHyOD/f91iAA).
### Explanation
Observe that the sequence can be obtained by combining three other sequences, one zero-indexed and the others one-indexed:
* Start with `0 0 0 0 2 2 2 2 4 4 4 4` = `a//4*2` (0-indexed);
* Add `1 2 2 1 1 2 2 1 1 2 2 1` = `aBA2|1`, where `BA` is bitwise AND, and `|` is logical OR (1-indexed);
* Multiply the sum by `-1 1 -1 1 -1 1 -1 1 -1 1 -1 1` = `(-1)**a` (1-indexed).
If we start with `a` 1-indexed, we can compute the 1-indexed parts first (reading the expression left to right) and then decrement `a` for the 0-indexed part. Using the builtin variable `v=-1`, we get
```
v**a*((aBA2|1)+--a//4*2)
```
To shave two more bytes, we have to use some precedence-manipulation tricks. We can eliminate the inner parentheses by replacing `+` with `+:` (equivalent to `+=` in a lot of languages). Any compute-and-assign operator is of very low precedence, so `aBA2|1+:--a//4*2` is equivalent to `(aBA2|1)+:(--a//4*2)`. Pip will emit a warning about assigning to something that isn't a variable, but only if we have warnings enabled.
The only thing that's lower precedence than `:` is `Y`, the yank operator.\* It assigns its operand's value to the `y` variable and passes it through unchanged; so we can eliminate the outer parentheses as well by yanking the value rather than parenthesizing it: `YaBA2|1+:--a//4*2`.
\* `P`rint and `O`utput have the same precedence as `Y`ank, but aren't useful here.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~8~~ 7 bytes
```
H^Ḃ~N⁸¡
```
This uses the algorithm from [my Python answer](https://codegolf.stackexchange.com/a/113347/12012), which was improved significantly by [@GB](https://codegolf.stackexchange.com/users/18535/g-b).
[Try it online!](https://tio.run/nexus/jelly#AR4A4f//SF7huIJ@TuKBuMKh/zI04bi2wrXFvMOH4oKsR/8 "Jelly – TIO Nexus")
### How it works
```
H^Ḃ~N⁸¡  Main link. Argument: n
H        Halve; yield n/2. This returns a float, but ^ will cast it to int.
  Ḃ      Bit; yield n%2.
 ^       Apply bitwise XOR to both results.
   ~     Take the bitwise NOT.
    N⁸¡  Negate the result n times.
```
[Answer]
# Java 8, 19 bytes
```
n->~(n/2)+n%2*(n|2)
```
# Java 7, ~~47~~ 37 bytes
```
int c(int n){return~(n/2)+n%2*(n|2);}
```
First time Java (8) actually competes and is shorter than some other answers. Still can't beat the actual golfing languages like Jelly and alike, though (duhuh.. what a suprise.. >.> ;P)
0-indexed  
Port from [*@Xnor*'s Python 2 answer](https://codegolf.stackexchange.com/a/113355/52210).  
-10 bytes thanks to *@G.B.*
[Try it here.](https://tio.run/nexus/java-openjdk#LY7BDoIwEETvfMWExKS1BpEr@AmeOBoPFdE0gYW0i4nB@uvYEg6zO4c3k1maTjuHy5wAjjWbBoYYjYiX5Gxbniz9BB0LqWhX7AV9C1n6JPDjdO8Cv8Xeg3mg14ZEzdbQ63qDlrEWeA527TM4Iy/Dq3DKo1FqI4D647jts2HibAxxFmGChEJ6QCrLFfJJlF@WPw)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~15~~ ~~12~~ 11 bytes
```
Ḷ^1‘ż@N€Fị@
```
[Try it online!](https://tio.run/nexus/jelly#@/9wx7Y4w0cNM47ucfB71LTG7eHubof/RiaHtgYd3XO4HSji/h8A "Jelly – TIO Nexus")
### How it works
```
Ḷ^1‘ż@N€Fị@  Main link. Argument: n
Ḷ            Unlength; yield [0, 1, 2, 3, ..., n-1].
 ^1          Bitwise XOR 1; yield [1, 0, 3, 2, ..., n-1^1].
   ‘         Increment; yield [2, 1, 4, 3, ..., (n-1^1)+1].
      N€     Negate each; yield [-1, -2, -3, -4, ..., -n].
    ż@       Zip with swapped arguments; 
             yield [[-1, 2], [-2, 1], [-3, 4], [-4, 3], ..., [-n, (n-1^1)+1]].
        F    Flatten, yield [-1, 2, -2, 1, -3, 4, -4, 3, ..., -n, (n-1^1)+1].
         ị@  At-index with swapped arguments; select the item at index n.
```
[Answer]
# [RProgN 2](https://github.com/TehFlaminTaco/RProgN-2), ~~31~~ ~~25~~ 22 bytes
```
nx=x2÷1x4%{+$-1x^*}#-?
```
## Explained
```
nx=                         # Convert the input to a number, set x to it.
   x2÷                      # Floor divide x by 2.
      1                     # Place a 1 on the stack.
       x4%{       }#-?      # If x%4 is 0, subtract 1 from x//2, otherwise...
           +                # Add the 1 and the x together.
            $-1             # Push -1
               x^           # To the power of x.
                 *          # Multiply x//2+1 by -1^x. (Invert if odd, do nothing if even)
```
[Try it online!](https://tio.run/nexus/rprogn-2#@59XYVthdHi7YYWJarW2iq5hRZxWrbKu/f///3V1U4E45b8FAA "RProgN 2 – TIO Nexus")
[Answer]
# Ruby, ~~26 23~~ 18 bytes
```
->n{~n/2+n%2*n|=2}
```
0-based
-3 bytes stealing the -1^n idea from [Greg Martin](https://codegolf.stackexchange.com/a/113348/18535), [Dennis](https://codegolf.stackexchange.com/a/113347/18535) and maybe somebody else, then -5 bytes stealing the n|2 idea from [xnor](https://codegolf.stackexchange.com/a/113355/18535).
[Answer]
# [Stacked](https://github.com/ConorOBrien-Foxx/stacked), ~~30~~ 28 bytes
```
:2%([:2/\4%2=tmo+][1+2/_])\#
```
[Try it online!](https://tio.run/nexus/stacked#MzRUqLNTqFZIVbBSSP1vZaSqEW1lpB9jompkW5Kbrx0bbahtpB8fqxmj/F8RqChGQVlHoVYhN7FAISWzuOA/AA "Stacked – TIO Nexus") Returns a function, which as allowed [per meta consensus.](https://codegolf.meta.stackexchange.com/a/11317/31957). Takes input from the top of the stack.
Using the same approach as the [RProgN 2 answer](https://codegolf.stackexchange.com/a/113343/31957).
---
Alternatively, 46 bytes. [Try it online!](https://tio.run/nexus/stacked#MzRUqLNTqFZIVbBSSP1fraihaRhtZWWorWNVlFqmE1MMJGN0Yoy0Y/O0HPTyFAx1lfMUjFRLcvO14mv/KwK1xSgo6yjUKuQmFiikZBYX/AcA "Stacked – TIO Nexus"):
```
{!()1[::1+,:rev,\srev\,\2+]n*@.n 1-#n 2%tmo*_}
```
This one generates the range then selects and negates the member as appropriate.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~44~~ ~~33~~ 27 bytes
```
lambda n:(-1)**n*~(n/2^n%2)
```
*Thanks to @GB for golfing off 6 bytes!*
[Try it online!](https://tio.run/nexus/python2#BcFBCoMwEAXQdT3F3wzOBKV07EqwJylCSkwR9FeCa6@evpcx4V23uH9SBEftHxYCw6W8@0xxq/lXQKxEifwu6k8bm9tRVp5oxRP6F2RILQTKDllpVv8 "Python 2 – TIO Nexus")
[Answer]
# 05AB1E, 8 bytes
```
2‰`^±¹F(
```
[**Try it online**](https://tio.run/nexus/05ab1e#@2/0qGFDQtyhjYd2umn8/28CAA)
**Explanation**
```
2‰          divmod by 2
  `         flatten list
   ^        XOR
    ±       NOT
     ¹F(    Push 1st argument, loop N times, negate
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~9~~ 8 bytes
```
|2×Ḃ+H~$
```
[Try it online!](https://tio.run/nexus/jelly#@19jdHj6wx1N2h51Kv///zcyBgA "Jelly – TIO Nexus")
*-1 thanks to [Dennis](/users/12012). Duh float conversions.*
Uses @xnor's Python 2 approach.
**EDIT**: [>\_>](/a/113373/41024)
[Answer]
## [CJam](https://sourceforge.net/p/cjam), 16 bytes
```
{_(_1&)^2/)W@#*}
```
1-based input.
[Try it online!](https://tio.run/nexus/cjam#MzKo1vxfHa8Rb6imGWekrxnuoKxV@78uPzi/Vv8/AA "CJam – TIO Nexus")
### Explanation
Here is a breakdown of the code with the values on the stack for each input from `1` to `4`. The first few commands only affect the two least significant bits of `n-1` so after `4`, this stuff just repeats cyclically, with the results incremented by 2, due to the halving.
```
Cmd             Stack: [1]       [2]       [3]       [4]
_    Duplicate.        [1 1]     [2 2]     [3 3]     [4 4]
(    Decrement.        [1 0]     [2 1]     [3 2]     [4 3]
_    Duplicate.        [1 0 0]   [2 1 1]   [3 2 2]   [4 3 3]
1&   AND 1.            [1 0 0]   [2 1 1]   [3 2 0]   [4 3 1]
)    Increment.        [1 0 1]   [2 1 2]   [3 2 1]   [4 3 2]
^    XOR.              [1 1]     [2 3]     [3 3]     [4 1]
2/   Halve.            [1 0]     [2 1]     [3 1]     [4 0]
)    Increment.        [1 1]     [2 2]     [3 2]     [4 1]
W    Push -1.          [1 1 -1]  [2 2 -1]  [3 2 -1]  [4 1 -1]
@    Rotate.           [1 -1 1]  [2 -1 2]  [2 -1 3]  [1 -1 4]
#    -1^n.             [1 -1]    [2 1]     [2 -1]    [1 1]
*    Multiply.         [-1]      [2]       [-2]      [1]
```
[Answer]
# [Perl 6](https://perl6.org), ~~55 27 24~~ 22 bytes
```
{(-1,2,-2,1,{|($^a,$^b,$^c,$^d Z+ -2,2,-2,2)}...*)[$_]}
```
(Inspired by the Haskell `zipWith` answer)  
[Try it](https://tio.run/nexus/perl6#Ky1OVSgz00u25sqtVFBLzk9JVbD9X62ha6hjpKNrpGOoU12joRKXqKMSlwTEyUCcohClrQCUAssbadbq6elpaUarxMfW/i8oLVFIyVdIyy9SiDMyUdC1U4jJzAMJViuATNZQAPM0FWr/AwA "Perl 6 – TIO Nexus")
```
{+^($_ div 2)+$_%2*($_+|2)}
```
(Inspired by several answers)  
[Try it](https://tio.run/nexus/perl6#Ky1OVSgz00u25sqtVFBLzk9JVbD9X60dp6ESr5CSWaZgpKmtEq9qpAXka9cYadb@LygtUUjJV0jLL1KIMzJR0LVTiMnMAwlWK4B0ayiAeZoKtf8B "Perl 6 – TIO Nexus")
```
{+^($_+>1)+$_%2*($_+|2)}
```
[Try it](https://tio.run/nexus/perl6#Ky1OVSgz00u25sqtVFBLzk9JVbD9X60dp6ESr21nqKmtEq9qpAXi1Bhp1v4vKC1RSMlXSMsvUogzMlHQtVOIycwDCVYrgLRqKIB5mgq1/wE "Perl 6 – TIO Nexus")
```
{+^$_+>1+$_%2*($_+|2)}
```
[Try it](https://tio.run/nexus/perl6#Ky1OVSgz00u25sqtVFBLzk9JVbD9X60dpxKvbWeorRKvaqSlAWTXGGnW/i8oLVFIyVdIyy9SiDMyUdC1U4jJzAMJViuANGoogHmaCrX/AQ "Perl 6 – TIO Nexus")
## Expanded:
```
{  # bare block lambda with implicit parameter ｢$_｣
    +^          # numeric binary invert the following
      $_ +> 1   # numeric bit shift right by one
  +
      $_ % 2    # the input modulo 2
    *
      ($_ +| 2) # numeric binary inclusive or 2
}
```
(All are 0 based)
[Answer]
# [Haskell](https://www.haskell.org/), ~~37~~ 36 bytes
```
(([1,3..]>>= \x->[-x,x+1,-x-1,x])!!)
```
[Try it online!](https://tio.run/nexus/haskell#@59mq6ERbahjrKcXa2dnqxBToWsXrVuhU6FtqKNboWuoUxGrqaio@T83MTNPwVahKDUxxSdPAaSyoCgzr0RBTyHtvzEA "Haskell – TIO Nexus") This is an anonymous function which takes one number `n` as argument and returns 0-indexed the `n`th element of the sequence.
[Answer]
# Haskell, 56 bytes
```
f n=concat(iterate(zipWith(+)[-2,2,-2,2])[-1,2,-2,1])!!n
```
0-indexed
[Answer]
# Perl 5 47 + 1 (for flag) = 48 Bytes
```
print(((sin$_%4>.5)+1+2*int$_/4)*($_%4&1?1:-1))
```
### Old Submission 82 Bytes
```
@f=(sub{-$_[0]},sub{$_[0]+1},sub{-$_[0]-1},sub{$_[0]});print$f[$_%4](1+2*int$_/4)
```
Run like so:
```
perl -n <name of file storing script>  <<<  n
```
[Answer]
# JavaScript (ES7), 28 bytes
```
n=>(n+2>>2)*2*(-1)**n-!(n&2)
```
1-indexed. I haven't looked at any other answers yet so I don't know if this is the best algorithm, but I suspect not.
[Answer]
# JavaScript, ~~28~~ 22 bytes
Thanks @ETHproductions for golfing off 6 bytes
```
x=>x%2?~x>>1:x%4+x/2-1
```
[Try it online!](https://tio.run/nexus/javascript-node#S7P9n2ZbYWtXoWpkX1dhZ2doVaFqol2hb6Rr@D/PNtpQx0jHWMdEx1THTMdcx0LHUsfQIJYrOT@vOD8nVS8nP10jTy83sUAjTVPzPwA "JavaScript (Node.js) – TIO Nexus")
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 98 bytes
```
?sa0sb1sq[lq1+dsqla!<i3Q]sf[lb1-lfx]su[lblfx]sx[lb1+dsblfx]sj[lqdd4%d0=u1=j2%1=xljxlfx]dsix_1la^*p
```
Gosh, this is the longest answer here, mainly because I went the path of generating the absolute value of each element of the sequence one by one based on the following recursive formula:
[![enter image description here](https://i.stack.imgur.com/X1g8j.png)](https://i.stack.imgur.com/X1g8j.png)
then outputting `(-1)^n * a_n`, rather than directly computing the `n`'th element. Anyways, this is `1`-indexed.
[Try it online!](https://tio.run/nexus/dc#@29fnGhQnGRYXBidU2ionVJcmJOoaJNpHBhbnBadk2Som5NWEVtcCmSCGRUgMaAqCC8LqCclxUQ1xcC21NA2y0jV0LYiJ6sCJJdSnFkRb5iTGKdV8P@/yde8fN3kxOSMVAA "dc – TIO Nexus")
[Answer]
# R, 38 bytes
```
function(n)floor(n/2+1-2*!n%%4)*(-1)^n
```
Explanation
```
floor(n/2+1)                ->  1 2  2 3  3 4  4 5...
floor(n/2+1-2*!n%%4)        ->  1 2  2 1  3 4  4 3... (subtract 2 where n%%4 == 0)
floor(n/2+1-2*!n%%4)*(-1)^n -> -1 2 -2 1 -3 4 -4 3... (multiply odd n by -1)
```
[Answer]
# TI-Basic (TI-84 Plus CE), 31 bytes
```
.5(Ans+1+remainder(Ans+1,2)-4not(remainder(Ans,4)))i^(2Ans
```
TI-Basic is a tokenized language and each token used here is one byte, except `remainder(`, which is two.
This uses the 1-indexed version.
Explanation:
There is a pattern that repeats every four numbers. In the 1-indexed version, it is: -(x+1)/2, (x+1)/2, -(x+1)/2, (x-1)/2 for the input value x. This can be represented as a piecewise-defined function.
f(x) = -(x+1)/2 if x ≡ 1 mod 4; (x+1)/2 if x ≡ 2 mod 4; -(x+1)/2 if x ≡ 3 mod 4; (x-1)/2 if x ≡ 0 mod 4
Because the "x ≡ 1 mod 4" and "x ≡ 3 mod 4" parts are the same, we can combine them into "x ≡ 1 mod 2".
Now are piecewise function is:
f(x) = -(x+1)/2 if x ≡ 1 mod 2; (x+2)/2 if x ≡ 2 mod 4; (x-2)/2 if x ≡ 0 mod 4
This is where I start breaking it into actual commands. Since the value is positive for even indexes and negative for odd ones, we can use (-1)^x. However, in TI-Basic `i^(2X` (5 bytes) is shorter than `(-1)^Ans` (6 bytes). Note that parentheses are required due to order of operations.
Now that we have the way to negate the odd inputs out of the way, we move on to the mods (adding the negating back on later). I made the case of an odd input the default, so we start with `.5(Ans+1)`.
To fix the case of even input, just add one to the number in the parentheses, but only when x ≡ 0 mod 2. This could be represented as `.5(Ans+1+remainder(Ans+1,2))` or `.5(Ans+1+not(remainder(Ans,2)))`, but they have the same byte count, so it doesn't matter which.
To fix the case of multiple-of-4 input, we need to subtract 3 from the number in the parentheses, but also another 1 because all multiples of 4 are even, which would add one from our previous step, so we now have `.5(Ans+1+remainder(Ans+1,2)-4not(remainder(Ans,4)))`.
Now, just tack on the sign-determining part to the end to get the full program.
[Answer]
# Befunge 93, 25 bytes
Zero-indexed
```
&4/2*1+&4%3%!!+&2%2*1-*.@
```
[Try it Online!](https://tio.run/nexus/befunge#@69mom@kZaitZqJqrKqoqK1mpArk6mrpOfz/bwoA)
The number is given by `((2(n / 4) + 1) + !!((n % 4) % 3)) * (2(n % 2) - 1)`
[Answer]
## [QBIC](https://drive.google.com/drive/folders/0B0R1Jgqp8Gg4cVJCZkRkdEthZDQ), 53 bytes
```
b=1:{[b,b+3|~b=a|_x(-(b%2)*2+1)*(q+(b%4>1)*-1)]]q=q+2
```
Explanation:
```
b=1     Set b to a starting value of 1
        QBIC would usually use the pre-initialised variable q, but that is already in use
:       Get an input number from the cmd-line, our term to find
{       Start an infinite loop
[b,b+3| FOR-loop: this runs in groups of 4, incrementing its own bounds between runs
~b=a|   If we've reached the term requested
_x      QUIT the program and print:
(-(b%2)*2+1)   The b%2 gives a 1 or a 0, times 2 (2,0), negged (-2,0) and plus one (-1,1)
*              That gives us the sign of our term. Now the value:
(q+(b%4>1)*-1) This is q + 1 if the inner loop counter MOD 4 (1,2,3,0...) is 2 or 3.
]       Close the IF that checks the term
]       Close the FOR-loop
q=q+2   And raise q by 2 for the next iteration of the DO-loop.
```
[Answer]
# [Wise](https://github.com/Wheatwizard/Wise), 19 bytes
```
::>!:><^^~![!-!-~]|
```
[Try it Online!](https://tio.run/nexus/wise#@29lZadoZWcTF1enGK2oq6hbF1vz//9/EwA)
This is just a port of [@Dennis' Jelly answer](https://codegolf.stackexchange.com/a/113373/62574) to Wise.
[Answer]
## Q, 52 bytes
```
{(1 rotate(,/){x,(-)x}each 1_((_)x%4)+til 3)x mod 4}
```
0 indexed solution.
1. Gets the block number ie. which [-x x+1 -(x+1) x] block within the sequence contains the index.
2. Gets the index of the value within the block based on the index of the value within the whole sequence.
3. Creates the block.
4. Indexes into it via index derived in step 2.
]