Question: This function given a non empty array A of N intergers(sorted in non decreasing order) and integer K, checks whether A contains numbers 1,2...K (every

This function given a non empty array A of N intergers(sorted in non decreasing order) and integer K, checks whether A contains numbers 1,2...K (every number from 1 to K at least once)and no other number

 

function solution(A, K) {

    var n = A.length;

    for (var i = 0; i < n - 1; i++) {

        if (A[i] + 1 < A[i + 1])

            return false;

    }

    if (A[0] != 1 && A[n - 1] != K)

        return false;

    else

        return true;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!