Sort by

recency

|

27 Discussions

|

  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Modify The Sequence Solution

  • + 1 comment

    def modifySequence(arr):

        N = len(arr)
    M = (N+1)*[0]
    L = 0
    for i in range(0,N):
        if arr[i] < i + 1:
            continue
        if arr[i] >= arr[M[L]] + i - M[L] :
            j = L+1        
        else:
            lower = 1
            upper = L
            while lower <= upper:
                mid = int((upper+lower)/2)
                if arr[M[mid]] <= arr[i] - i + M[mid]:
                    lower = mid + 1
                else:
                    upper = mid - 1
    
            j = lower
    
        M[j] = i
    
        if j > L:
            L = j
    
    return len(arr)-L
    
  • + 0 comments

    Here is Modify the sequence problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-modify-the-sequence-problem-solution.html

  • + 1 comment
    def modifySequence(arr):
        c=0
        # Write your code here
        for v,i in enumerate(arr):
            if v in range(len(arr)-1):
                if arr[v]>=arr[v+1]:
                   c=c+1
                   x = random.randint(arr[v],arr[v+2])
                   k=1 if x==arr[v+1] else 0
                   arr[v+1]=x
                   if k==1:
                    c=c+1
        return c
    
  • + 0 comments

    Could someone give me a hint or a useful midrange test case? All the small ones pass and big ones are too big to debug. At each step I'm considering the possibilities that either current number in sequence is changed or last one is changed or nothing is changed. I'm going to add my code, would appreciate if someone would be kind enough to provide a test case that is not too big where my code fails.

    public static int modifySequence(List<Integer> arr) {
            int changedValue = 1;
            int noChange = arr.get(0);
            // count if this changes
            int cc = changedValue == noChange ? 0 : 1;
            // count if current does not change
            int cnc = 0;
            for (int i = 1; i < arr.size(); i++) {
                if (arr.get(i) <= noChange) {
                    // Can work if previous value changed
                    if (arr.get(i) > changedValue) {
                        changedValue = noChange + 1;
                        cc = cnc + 1;
                        noChange = arr.get(i);
                        cnc = cc;
                    // This value definitely needs to change
                    } else {
                        changedValue = (cc < cnc ? changedValue : noChange) + 1;
                        cc = Math.min(cc, cnc) + 1;
                        noChange = changedValue;
                        cnc = cc;
                    }
                // No need to change
                } else {
                    changedValue = (cc < cnc ? changedValue : noChange) + 1;
                    noChange = arr.get(i);
                    cc = Math.min(cc, cnc) + (changedValue == noChange ? 0:1);
                }
            }
            return Math.min(cc,cnc);
        }