Correctness and the Loop Invariant

Sort by

recency

|

200 Discussions

|

  • + 0 comments

    Emergency Locksmith provides 24/7 urgent locksmith services for homes, offices, and vehicles. Whether you’re locked out, facing a broken lock, or need immediate repairs, skilled professionals arrive quickly to secure your property. Just like emergency tree services handle unexpected situations to restore safety and order, Emergency Locksmith ensures rapid response and reliable solutions when you need them most, keeping your belongings and loved ones safe with trusted expertise and prompt action.

  • + 0 comments

    Go code is empty :/

  • + 0 comments

    Here is problem solution in python java c++ c and javascript - https://programmingoneonone.com/hackerrank-correctness-and-the-loop-invariant-solution.html

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/G0rl1U-fDRY

    void insertionSort(int N, int arr[]) {
        int i,j;
        int value;
        for(i=1;i<N;i++)
        {
            value=arr[i];
            j=i-1;
            while(j>=0 && value<arr[j])
            {
                arr[j+1]=arr[j];
                j=j-1;
            }
            arr[j+1]=value;
        }
        for(j=0;j<N;j++)
        {
            printf("%d",arr[j]);
            printf(" ");
        }
    }
    
  • + 0 comments

    wtf happen to my editor? my Typescript just only main() and have no input, nothing.

    function main() {
        // Enter your code here
    }
    

    update my answer

    /**
     * Typescript was corrupted and have no any codes, i was switch to Javascript
     * and copy it function to this test
     */
    function loopInvariant(ar: number[]): void {
        for (let i = 1; i < ar.length; i++) {
            var value = ar[i];
            var j = i - 1;
    
            //while (j > 0 && ar[j] > value) { // Wrong here. changes "j > 0" to "j >= 0"
            while (j >= 0 && ar[j] > value) {
                ar[j + 1] = ar[j];
                j = j - 1;
            }
            ar[j + 1] = value;
        }
        console.log(ar.join(' '));
    }
    
    function main() {
        const n: number = parseInt(readLine().trim(), 10);
    
        const arr: number[] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
    
        loopInvariant(arr);
    }