g | x | w | all
Bytes Lang Time Link
020C++ gcc250208T060103Zl4m2
019Rust250206T020438Z138 Aspe
019C gcc181109T185913ZArnauld
017Node.js181109T163208ZArnauld

C++ (gcc), s=20

#include <bitset>
typedef std::bitset<256> bs;
bs addbit(bs x, int k) {
	x[k] = 1;
	return x;
}
template<int s> int maxn = 0;
template<int s> bs maxseg;
const bs one = 1;
template<int s, int n=0>
void f(bs all, bs seg) {
	for (int i = 1; i <= s; ++i) {
		seg <<= 1;
		if ((all & seg).any()) continue;
		// % avoids infinite instances
		f<s, (n+1)%(s+1)>(all | seg, seg | one);
	}
	if (maxn<s> < n) maxn<s> =n, maxseg<s> =seg;
}
template<int k>
void g() {
	if (k) g<k-!!k>();
	f<k>(one, one);
	for (int i=0; i<256; ++i) putchar('.'+!!maxseg<k>[i]);
	printf("(%d->%d)\n", k, maxn<k>);
}
int main() {
	g<20>();
}

Try it online!

Output:

................................................................................................................................................................................................................................................................(0->0)
.//.............................................................................................................................................................................................................................................................(1->1)
.././/..........................................................................................................................................................................................................................................................(2->2)
..././..//......................................................................................................................................................................................................................................................(3->3)
..../..././/....................................................................................................................................................................................................................................................(4->3)
...../..../..././/..............................................................................................................................................................................................................................................(5->4)
....../...../.../...././/.......................................................................................................................................................................................................................................(6->5)
......./.../..../...../......//./...............................................................................................................................................................................................................................(7->6)
......../.../......./...../.....././/...........................................................................................................................................................................................................................(8->6)
........./...../....../..../......../......././/................................................................................................................................................................................................................(9->7)
........../...../..../....../......./......../........./..//....................................................................................................................................................................................................(10->8)
.........../......./..../......../........./........../..././/..................................................................................................................................................................................................(11->8)
............/........./......../..../......./.........../........../..././/.....................................................................................................................................................................................(12->9)
............./...../......./........./....../............/......../.........../........../...//.................................................................................................................................................................(13->10)
............../...../.........../......./....../......../............/........./........../............./.././..................................................................................................................................................(14->11)
.............../......./......../........./............../............/........../.........../............./..././/.............................................................................................................................................(15->11)
................/.........../............../........./......./.............../............./......../............/..././/.......................................................................................................................................(16->11)
................./........../......./.............../.........../........./............../................/............/............./..././/...................................................................................................................(17->12)
.................././........./............./............/................./................/.............../......../........../......./............../.....//.................................................................................................(18->13)
.................../............./............/................../........./........../............../................./................/......./.............../..././/........................................................................................(19->13)
..................../../......../............/......./................./................../.............../.................../............./............../................/...../...//........................................................................(20->14)

real    0m27.938s
user    0m27.937s
sys 0m0.000s

2018~2025 cpufreq should less than doubled so this is valid

Rust, s = 19

Rust port of @Arnauld's C answer.

Run it on RustPlayground!

use std::fmt::Write; // for write! macro used to build strings

/// Search function similar to the C version
fn search(
    arr: &mut [u8],
    n: i32,
    sum: &mut [u8],
    max_depth: &mut i32,
    result_str: &mut String,
    depth: i32,
) {
    // If we found a better array, store it in `result_str`.
    if depth > *max_depth {
        // Clear and build the new result string.
        result_str.clear();
        for i in 0..depth {
            write!(result_str, "{} ", arr[i as usize]).unwrap();
        }
        *max_depth = depth;
    }

    // Try to append i = 1..=n to the current array.
    for i in 1..=n {
        // Provided that doing so does not produce a sum that was already encountered.
        if sum[i as usize] == 0 {
            // In C: for(sum[s = i] = 1, j = depth; j && !sum[s += arr[j - 1]]; j--) {}
            // We translate that logic to Rust step by step:
            let mut s = i as i32;
            sum[s as usize] = 1;

            let mut j = depth;
            while j > 0 {
                let idx = s + arr[(j - 1) as usize] as i32;
                if sum[idx as usize] != 0 {
                    break;
                }
                s = idx;
                sum[s as usize] = 1;
                j -= 1;
            }

            // If j == 0, we managed to add i in a valid position; recurse deeper.
            if j == 0 {
                arr[depth as usize] = i as u8;
                search(arr, n, sum, max_depth, result_str, depth + 1);
            }

            // Clear the flags we set in this attempt.
            // In C: for(sum[s = i] = 0, k = depth - 1; k >= j; k--) { sum[s += arr[k]] = 0; }
            let mut s = i as i32;
            sum[s as usize] = 0;

            let mut k = depth - 1;
            while k >= j {
                s += arr[k as usize] as i32;
                sum[s as usize] = 0;
                k -= 1;
            }
        }
    }
}

/// Solve function similar to the C version.
/// Returns the maximum depth for a given `n`, and the construction in `result_str`.
fn solve(n: i32, result_str: &mut String) -> i32 {
    let mut max_depth = 0;
    // Highest possible sum for n
    let hi = n * (n + 1) / 2;
    // Encountered sums
    let mut sum = vec![0u8; (hi + 1) as usize];
    // Our working array
    let mut arr = vec![0u8; n as usize];

    search(&mut arr, n, &mut sum, &mut max_depth, result_str, 0);
    max_depth
}

fn main() {
    let mut result_str = String::new();

    for n in 1..=19 {
        let res = solve(n, &mut result_str);
        println!("N = {} --> {} with [ {}]", n, res, result_str);
    }
}

C (gcc), s = 19

This is basically a port of my Node.js answer. It goes one step further with the default compiler options and two steps further with -O2 or -O3.

#include <stdio.h>
#include <string.h>
#include <stdint.h>

void search(uint8_t * arr, int n, uint8_t * sum, int * max, char * str, int depth) {
  int i, j, k, s;
  char tmp[16];

  // do we have a better array?
  if(depth > *max) {
    for(str[0] = '\0', i = 0; i < depth; i++) {
      sprintf(tmp, "%d ", arr[i]);
      strcat(str, tmp);
    }
    *max = depth;
  }

  // try to append i = 1 to n to the current array
  for(i = 1; i <= n; i++) {
    // provided that doing so does not produce a sum that was already encountered
    if(!sum[i]) {
      for(sum[s = i] = 1, j = depth; j && !sum[s += arr[j - 1]]; j--) {
        sum[s] = 1;
      }
      if(!j) {
        // this is valid: set the value in arr[] and do a recursive call
        arr[depth] = i;
        search(arr, n, sum, max, str, depth + 1);
      }
      // whether the recursive call was processed or not, we have to clear the flags
      // that have been set above
      for(sum[s = i] = 0, k = depth - 1; k >= j; k--) {
        sum[s += arr[k]] = 0;
      }
    }
  }
}

int solve(int n, char * str) {
  int     max = 0;              // best length so far
  int     hi = n * (n + 1) / 2; // highest possible sum for n
  uint8_t sum[hi + 1];          // encountered sums
  uint8_t arr[n];               // array

  memset(sum, 0, (hi + 1) * sizeof(uint8_t));
  search(arr, n, sum, &max, str, 0);

  return max;
}

/////////////////////////////////////////////////////////////////////////////////////

void main() {
  char str[128];
  int  n, res;

  for(n = 1; n <= 19; n++) {
    res = solve(n, str);
    printf("N = %d --> %d with [ %s]\n", n, res, str);
  }
}

Try it online!

Node.js, s = 17

Just a simple recursive search to get the ball rolling. Returns both the length and a valid array.

solve = n => {
  var max = 0,  // best length so far
      best,     // best array so far
      sum = {}; // encountered sums

  (search = a => {
    var i, j, k, s;

    // do we have a better array?
    if(a.length > max) {
      max = a.length;
      best = [...a];
    }

    // try to prepend i = 1 to n to the current array
    for(i = 1; i <= n; i++) {
      // provided that doing so does not produce a sum that was already encountered
      if((sum[j = 0, s = i] ^= 1) && a.every(n => sum[j++, s += n] ^= 1)) {
        search([i, ...a]);
      }
      // reset the flags that have been toggled above
      for(sum[s = i] ^= 1, k = 0; k < j; k++) {
        sum[s += a[k]] ^= 1;
      }
    }
  })([]);

  return [ max, best ];
}

Try it online!