• + 0 comments
    public static void whatsNext(List<long> arr)
    {
        var tzc = 0L;
        
        if (arr.Count % 2 == 0) {
            tzc = arr[^1];
            arr.RemoveAt(arr.Count - 1);
        }
        
        var toc = arr[^1];
        
        if (arr.Count > 1) {
            --arr[^2];
        }
        
        arr[^1] = 1;
        arr.Add(1 + tzc);
        arr.Add(toc - 1);
        
        for (var i = 1; i < arr.Count - 1; ) {
            if (arr[i] == 0) {
                arr[i - 1] += arr[i + 1];
                arr.RemoveRange(i, 2);
            } else {
                ++i;
            }
        }
        
        if (arr[^1] == 0) {
            arr.RemoveAt(arr.Count - 1);
        }
        
        Console.WriteLine(arr.Count);
        Console.WriteLine(string.Join(' ', arr));
    }