Sort by

recency

|

44 Discussions

|

  • + 0 comments

    C template is wrong: it uses int instead of long

  • + 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));
    }
    
  • + 0 comments

    I don't try to compress code. So you cat read it and make observations.

    def whatsNext(arr):
        if len(arr)==0:
            ret = []
        elif len(arr)==1:
            arr[-1] -= 1
            ret = [1]+[1]+[arr[-1]]
        elif len(arr)==2:
            arr[-1] += 1
            arr[-2] -= 1
            ret = [1]+[arr[-1]]+[arr[-2]]
        elif len(arr)%2!=0:
            arr[-1] -= 1
            arr[-2] -= 1
            ret = arr[:-1]+[1]+[1]+[arr[-1]]
        else:
            arr[-1] += 1
            arr[-2] -= 1
            arr[-3] -= 1
            ret = arr[:-2]+[1]+[arr[-1]]+[arr[-2]]
        if len(ret)>2:
            for i in range(1,len(ret)-1):
                if ret[i]==0 and ret[i+1]==1:
                    ret[i-1] += 1
                    ret[i+1] = None
        ret = list(filter(bool,ret))
        print(len(ret))
        print(*ret)
    
  • + 0 comments

    Here is What's Next? problem solution in Python java c++ and c programming -- https://programs.programmingoneonone.com/2021/07/hackerrank-whats-next-problem-solution.html

  • + 0 comments

    Why does the C++ stub code deal with int data ???!!!