%I #36 Jan 14 2023 02:06:06
%S 1,2,2,3,1,2,1,4,5,1,2,4,1,5,1,2,5,1,2,8,4,5,3,4,3,6,1,8,3,2,5,4,7,6,
%T 2,3,9,1,2,12,4,5,3,4,8,9,7,8,3,10,1,12,3,2,5,4,7,6,9,8,11,10,2,3,13,
%U 1,2,16,4,5,3,4,8,9,7,8,12,13,11,12,3
%N a(1) = 1; a(2) = 2; a(n) = n - max{k<n | a(k) = m(n)}, where m(n) is the largest term so far if a(n-1) is odd, the second largest term so far if a(n-1) is even.
%C From _M. F. Hasler_, Sep 29 2019:
%C The sequence may be seen as a table with rows of length |2n-1|, n = 0, 1, ...
%C Then from n = 5, a(18) = 1 on, the rows are of the form
%C row(n) = (1, 2, 2n-2, ((4k, 4k+1, 4k-1, 4k), k=1..(n-3)/2), 3, 2n-4) for odd n,
%C row(n) = (1, 2n-4, ((2k+1, 2k), k=1..n-3), 2, 3, 2n-3) for even n.
%C All rows n >= 5 start with a((n-1)^2 + 2) = 1, and there are no other '1's beyond a(15).
%C (End)
%H John Tyler Rascoe, <a href="/A327759/b327759.txt">Table of n, a(n) for n = 1..10000</a>
%F a(n) = 1 iff n is in {1, 5, 7, 10, 13, 15} union A059100 \ { 2, 3, 6, 11 }.
%e a(9) is odd. The largest term up to that point is 5. The largest index of 5 is 9. a(10) = 10 - 9 = 1.
%e a(16) is even. The second largest term up to that point is 4. The largest index of 4 is 12. a(17) = 17 - 12 = 5.
%e From _M. F. Hasler_, Sep 29 2019: (Start)
%e Written as a table with rows of length |2n-1|, n = 0, 1, ...:
%e 1, /* row n=0 */
%e 2, /* row n=1; from here on, length = 2n-1 */
%e 2, 3, 1, /* row n=2 */
%e 2, 1, 4, 5, 1, /* row n=3 */
%e 2, 4, 1, 5, 1, 2, 5, /* n=4 */
%e 1, 2, 8, 4, 5, 3, 4, 3, 6, /* n=5. Here starts the regular pattern. */
%e 1, 8, 3, 2, 5, 4, 7, 6, 2, 3, 9, /* n=6 */
%e 1, 2, 12, 4, 5, 3, 4, 8, 9, 7, 8, 3, 10, /* n=7 */
%e 1, 12, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 2, 3, 13, /* n=8 */
%e 1, 2, 16, 4, 5, 3, 4, 8, 9, 7, 8, 12, 13, 11, 12, 3, 14, /* n=9 */
%e 1, 16, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 2, 3, 17, /* n=10 */
%e ...
%e (End)
%t s={1, 2}; sm = 2; sm2 = 1; Do[a = Length[s] + 1 - If[OddQ[s[[-1]]], Position[s, _?(# == sm &)], Position[s, _?(# == sm2 &)]][[-1, 1]]; AppendTo[s, a]; If[a > sm, sm2 = sm; sm = a,If[a < sm && a > sm2, sm2 = a]], {100}]; s (* _Amiram Eldar_, Sep 28 2019 *)
%o (VBA/Excel)
%o Sub A327759()
%o Cells(1, 1) = 1
%o Cells(2, 1) = 2
%o For n = 3 To 1000
%o max1 = 0
%o For m = 1 To n - 1
%o If Cells(m, 1) >= max1 Then
%o max1 = Cells(m, 1)
%o m1 = m
%o End If
%o Next m
%o max2 = 0
%o For m = 1 To n - 1
%o If Cells(m, 1) <> max1 And Cells(m, 1) >= max2 Then
%o max2 = Cells(m, 1)
%o m2 = m
%o End If
%o Next m
%o If Cells(n - 1, 1) Mod 2 = 1 Then
%o Cells(n, 1) = n - m1
%o Else
%o Cells(n, 1) = n - m2
%o End If
%o Next n
%o End Sub
%o (PARI) A327759_upto(N=99, idx=[0,0], L, S, a)=vector(N,n,a=n-if(n>2,idx[2-a%2]); L<a && [S,idx[2]]=[L,idx[1]]; if(L<=a, L=a; idx[1]=n, S<=a, S=a; idx[2]=n); a) \\ _M. F. Hasler_, Sep 29 2019
%o (PARI) A327759(n)={my(r=sqrtint(abs(n-2))+1,c=n-(r-1)^2-1); if(n<17, digits(1223121451241512)[n], c==1, 1, c==2*r-2, 3,c==2*r-1, 2*r-3-r%2, r%2, if(c==3, 2*r-2, c>2, c\4*4+[0,1,-1,0][c-c\4*4+1], 2), c==2, 2*r-4, c<2*r-3, c\/2*2+(c%2)-2,2)} \\ _M. F. Hasler_, Sep 30 2019
%o (Python)
%o def A327759list(nmax):
%o A = [1,2]
%o for n in range(3,nmax+1):
%o if A[-1]%2 == 0:
%o A2 = list(set(A))
%o A2.sort()
%o m = A2[-2]
%o else: m = max(A)
%o i = len(A) - 1
%o while A[i] != m: i -= 1
%o A.append(n-i-1)
%o print(A) # _John Tyler Rascoe_, Jan 13 2023
%Y Cf. A059100 (indices of '1's, starting with 18), A141044 (col.1, starting at row 3).
%K nonn
%O 1,2
%A _Ali Sada_, Sep 24 2019