Chocolate Problem

Problem Link: https://www.codingninjas.com/codestudio/problems/chocolate-problem_893280

def findMinDiff(n, k, chocolates):
    # Write your code here.
    if(k>n):
        return -1
    chocolates.sort()
    ans=float('inf')
    i=0
    j=0
    while(j<n):
        if(j-i+1<k):
            j+=1
        elif(j-i+1==k):
            ans=min(ans,chocolates[j]-chocolates[i])
            i+=1
            j+=1
    return ans

Last updated