• + 0 comments
    def solve(n, k, s):
        # Write your code here
        valid_pairs = 0
        counter = [0]*(n+1)
        
        # at counter[i] there will be count of 1s upto i
        for i, bit in enumerate(s):
            counter[i + 1] = counter[i] + int(bit)
            
        p = 0
        for i, bit in enumerate(s):
            if bit == "1":
                p += counter[min(n, i + k + 1)] - counter[max(0, i - k)]
                         
        total_pairs = n*n           
        gcd_val = math.gcd(p, total_pairs)
        numerator = p//gcd_val
        denominator = total_pairs//gcd_val
        
        return f"{numerator}/{denominator}"